---
title: R语言-数据导入导出
author: 宇飞的世界
date: '2021-06-11'
slug: r-data-load-write
categories:
  - data load and write
tags:
  - readxl
  - openxlsx
  - readr
  - vroom
---

<script src="{{< blogdown/postref >}}index_files/header-attrs/header-attrs.js"></script>
<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
  { counter-reset: source-line 0; }
pre.numberSource code > span
  { position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
  { content: counter(source-line);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {  background-color: #f8f8f8; }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ef2929; } /* Alert */
code span.an { color: #8f5902; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #c4a000; } /* Attribute */
code span.bn { color: #0000cf; } /* BaseN */
code span.cf { color: #204a87; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4e9a06; } /* Char */
code span.cn { color: #000000; } /* Constant */
code span.co { color: #8f5902; font-style: italic; } /* Comment */
code span.cv { color: #8f5902; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #8f5902; font-weight: bold; font-style: italic; } /* Documentation */
code span.dt { color: #204a87; } /* DataType */
code span.dv { color: #0000cf; } /* DecVal */
code span.er { color: #a40000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #0000cf; } /* Float */
code span.fu { color: #000000; } /* Function */
code span.im { } /* Import */
code span.in { color: #8f5902; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #204a87; font-weight: bold; } /* Keyword */
code span.op { color: #ce5c00; font-weight: bold; } /* Operator */
code span.ot { color: #8f5902; } /* Other */
code span.pp { color: #8f5902; font-style: italic; } /* Preprocessor */
code span.sc { color: #000000; } /* SpecialChar */
code span.ss { color: #4e9a06; } /* SpecialString */
code span.st { color: #4e9a06; } /* String */
code span.va { color: #000000; } /* Variable */
code span.vs { color: #4e9a06; } /* VerbatimString */
code span.wa { color: #8f5902; font-weight: bold; font-style: italic; } /* Warning */
</style>

<div id="TOC">
<ul>
<li><a href="#data:read-write-description">数据导入导出</a>
<ul>
<li><a href="#readxl:description">readxl</a>
<ul>
<li><a href="#readxl:install">安装</a></li>
<li><a href="#readxl:usage">用法</a></li>
<li><a href="#batch:read">批量读取</a></li>
<li><a href="#批量输出">批量输出</a></li>
</ul></li>
<li><a href="#writexl">writexl</a>
<ul>
<li><a href="#writexl:usage">用法</a></li>
</ul></li>
<li><a href="#openxlsx:description">openxlsx</a>
<ul>
<li><a href="#openxlsx:install">安装</a></li>
<li><a href="#openxlsx:functions">基础功能</a></li>
<li><a href="#定制格式输出">定制格式输出</a></li>
<li><a href="#函数参数">函数参数</a></li>
<li><a href="#总结">总结</a></li>
</ul></li>
<li><a href="#readr:package">readr</a>
<ul>
<li><a href="#readr:install">安装</a></li>
<li><a href="#readr:usage">用法</a></li>
<li><a href="#readr:write-function">导出功能</a></li>
<li><a href="#总结-1">总结</a></li>
</ul></li>
<li><a href="#data:vroom">vroom</a>
<ul>
<li><a href="#安装">安装</a></li>
<li><a href="#用法">用法</a></li>
</ul></li>
<li><a href="#data:rstudio-addins">Rstudio导入</a>
<ul>
<li><a href="#rstudio-工具栏导入">rstudio 工具栏导入</a></li>
<li><a href="#插件导入">插件导入</a></li>
</ul></li>
<li><a href="#data:file-path">文件路径</a>
<ul>
<li><a href="#指定路径">指定路径</a></li>
<li><a href="#默认路径">默认路径</a></li>
</ul></li>
<li><a href="#data:expand">拓展</a>
<ul>
<li><a href="#tidyxl:package">tidyxl</a></li>
<li><a href="#参考资料">参考资料</a></li>
</ul></li>
</ul></li>
</ul>
</div>

<div id="data:read-write-description" class="section level1">
<h1>数据导入导出</h1>
<p>作为一名普通的数据分析师，我日常接触最多的数据是业务系统中的销售订单表、商品库存表、会员信息表，门店信息表，商品信息表等之类的业务表，但最初接触R时，看到的演示代码以及数据集大部分都是R包中内置的数据集，没有很明确操作数据的意义，没有代入感。在刚开始学习使用R做数据处理后，我就想使用自己的数据集来操作数据，用 R 去实现 Excel 的透视表或 sql 功能。这时就首先需要将原始数据导入<a href="#fn1" class="footnote-ref" id="fnref1"><sup>1</sup></a>R中。</p>
<p>现实生活中数据来源复杂，商业环境中数据源同样具有多样性，如 SAP,不同的数据库、OA系统、EXCEL 手工文件等;我们想要统一数据做分析，就需要将不同的数据源整合导入R中。</p>
<p>我们从读取方式简单区分为本地文件数据、数据库数据，本章主要说明常用的 Excel 文件和csv<a href="#fn2" class="footnote-ref" id="fnref2"><sup>2</sup></a>、txt 等文本文件的读写方式。关于数据库的数据的读取，可以参照后续 database <a href="#database">数据库章节</a>。</p>
<p>相信大家随便使用搜索引擎搜索诸如“将 Excel 导入 R”的关键词都能得到一些行之有效的方法，但是不够系统全面。本章主要简述R中数据导入导出的相关 R 包，如<code>readxl</code>,<code>writexl</code>,<code>openxlsx</code>,<code>readr</code>, <code>vroom</code>等主要处理 csv 或 Excel 的 R 包。</p>
<blockquote>
<p>当有其它数据格式需求的时候，那时候的你肯定已经会自行查找相关R包使用了。</p>
</blockquote>
<p>在本章开始前，假定已经有一些 R 相关基础。如使用 Rstudio 查看导入的数据，R 的数据结构等有一定认识。本章节主要分为:</p>
<ul>
<li><p>excel 读写</p></li>
<li><p>csv 等平面文件读写</p></li>
<li><p>文件路径</p></li>
</ul>
<div id="readxl:description" class="section level2">
<h2>readxl</h2>
<p>readxl 软件包使 R 获取 Excel 数据变得方便简洁。与现有的软件包(例如：xlsx)相比，readxl 没有外部依赖性，xlsx 等包依赖 java 环境。readxl 包容易在所有的操作系统安装使用。</p>
<p>readxl<a href="https://readxl.tidyverse.org/">项目地址</a>，本节大部分代码来源项目官网介绍，可自行查阅官网。</p>
<div id="readxl:install" class="section level3">
<h3>安装</h3>
<p>从 CRAN 安装最新发行版本的最简单方法是安装整个 tidyverse。</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">&quot;tidyverse&quot;</span>)</span></code></pre></div>
<blockquote>
<p>由于readxl不是tidyverse核心加载包，使用时仅需加载library(readxl)</p>
</blockquote>
<p>或者是从CRAN仅安装readxl;</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">&quot;readxl&quot;</span>)</span></code></pre></div>
<p>从github安装开发版：</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co"># install.packages(&quot;devtools&quot;)</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>devtools<span class="sc">::</span><span class="fu">install_github</span>(<span class="st">&quot;tidyverse/readxl&quot;</span>)</span></code></pre></div>
</div>
<div id="readxl:usage" class="section level3">
<h3>用法</h3>
<p>readxl 包的核心函数是读取函数<code>read_excel()</code>，该函数能将 Excel 文件数据加载到R中。readxl 包中包含了几个示例文件，我们在接下来的案例中使用。</p>
<p>1.读取</p>
<p>通过<code>readxl_example()</code>查看 readxl 包中自带 xlsx 文件。</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(readxl)</span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="fu">readxl_example</span>()</span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="fu">readxl_example</span>(<span class="st">&quot;clippy.xls&quot;</span>)</span></code></pre></div>
<p>需要注意<code>read_excel()</code>可读取 xls 和 xlsx 文件，函数会自动识别。</p>
<blockquote>
<p>xls 和 xlsx 的区别: xls 是 excel2003 及以前版本生成的文件格式，而 xlsx 是 excel2007 及以后版本生成的文件格式。xlsx 格式是向下兼容的，可兼容 xls 格式。老旧的业务使用的格式可能是 xls 格式。</p>
</blockquote>
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>xlsx_example <span class="ot">&lt;-</span> <span class="fu">readxl_example</span>(<span class="st">&quot;datasets.xlsx&quot;</span>) <span class="co">#返回 datasets.xlsx文件所在路径</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>dt <span class="ot">&lt;-</span> <span class="fu">read_excel</span>(xlsx_example)</span></code></pre></div>
<p>查看数据</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(dt)</span></code></pre></div>
<p>将 Excel 中的数据通过<code>read_excel()</code>读取保存到R中，该对象属于<code>tbl_df</code>类，是 tidyverse 系列的包数据框结构，是对 base R中的<code>data.frame</code>结构的改造，但是兼容<code>data.frame</code>。</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 查看dt类</span></span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a><span class="fu">class</span>(dt)</span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a><span class="co"># 查看数据类型</span></span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a><span class="fu">str</span>(dt)</span></code></pre></div>
<blockquote>
<p>关于<code>tbl_df</code>类，可以查阅<code>vignette("tibble")</code>。</p>
</blockquote>
<p>2.指定 sheet 读取</p>
<p>通过函数<code>excel_sheets()</code>查看 Excel 的sheet名称</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="fu">excel_sheets</span>(xlsx_example)</span></code></pre></div>
<p>当我们要读取 Excel 文件有多个 sheets 时，通过指定 sheet 参数,读取指定的 sheet，sheet 参数可以 是 sheet 的名称或顺序号。</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="fu">read_excel</span>(xlsx_example, <span class="at">sheet =</span> <span class="st">&quot;chickwts&quot;</span>)</span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a><span class="co"># not run</span></span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a><span class="co">#read_excel(xlsx_example, sheet = 1)</span></span>
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a><span class="co">#read_excel(xlsx_example, sheet = 3)</span></span></code></pre></div>
<p>3.指定读取范围</p>
<p>读取 Excel 文件指定范围。<code>read_excel()</code>中 range 参数接受单元格范围，最简单的表示方式即 Excle 中单元格表示方法,如 range = “D12:F15” or range = “R1C12:R6C15”。</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="fu">read_excel</span>(xlsx_example, <span class="at">sheet =</span> <span class="st">&quot;iris&quot;</span>)</span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a>dt1 <span class="ot">&lt;-</span> <span class="fu">read_excel</span>(xlsx_example, <span class="at">sheet =</span> <span class="st">&quot;iris&quot;</span>,<span class="at">range =</span> <span class="st">&#39;a1:e6&#39;</span>)</span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a>dt2 <span class="ot">&lt;-</span> <span class="fu">read_excel</span>(xlsx_example, <span class="at">sheet =</span> <span class="st">&quot;iris&quot;</span>,<span class="at">range =</span> <span class="st">&#39;R1C1:R6C5&#39;</span>)</span>
<span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-7"><a href="#cb10-7" aria-hidden="true" tabindex="-1"></a><span class="co"># dt1 dt2 是否相等</span></span>
<span id="cb10-8"><a href="#cb10-8" aria-hidden="true" tabindex="-1"></a><span class="fu">identical</span>(dt1,dt2)</span></code></pre></div>
<p>4.read_excel参数</p>
<p>readxl::read_excel 参数如下,请<code>?read_excel()</code>查看参数。</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="fu">read_excel</span>(path, <span class="at">sheet =</span> <span class="cn">NULL</span>, <span class="at">range =</span> <span class="cn">NULL</span>, <span class="at">col_names =</span> <span class="cn">TRUE</span>,</span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a>  <span class="at">col_types =</span> <span class="cn">NULL</span>, <span class="at">na =</span> <span class="st">&quot;&quot;</span>, <span class="at">trim_ws =</span> <span class="cn">TRUE</span>, <span class="at">skip =</span> <span class="dv">0</span>,</span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a>  <span class="at">n_max =</span> <span class="cn">Inf</span>, <span class="at">guess_max =</span> <span class="fu">min</span>(<span class="dv">1000</span>, n_max),</span>
<span id="cb11-4"><a href="#cb11-4" aria-hidden="true" tabindex="-1"></a>  <span class="at">progress =</span> <span class="fu">readxl_progress</span>(), <span class="at">.name_repair =</span> <span class="st">&quot;unique&quot;</span>)</span></code></pre></div>
<table>
<thead>
<tr class="header">
<th>read_excel参数</th>
<th>解释</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>path</td>
<td>xls或xlsx文件的路径</td>
</tr>
<tr class="even">
<td>sheet</td>
<td>默认是第一个sheet，可以是数字或sheet的名称</td>
</tr>
<tr class="odd">
<td>range</td>
<td>Excel中表示范围的字符，或者通过cell_类方法指定</td>
</tr>
<tr class="even">
<td>col_name</td>
<td>默认为TRUE,用第一行做列名;或者用默认名称,或者指定列名</td>
</tr>
<tr class="odd">
<td>col_types</td>
<td>默认为NULL，猜测各列类型，可指定各列的类型</td>
</tr>
<tr class="even">
<td>trim_ws</td>
<td>默认去掉表头字段中两边的空格</td>
</tr>
<tr class="odd">
<td>skip</td>
<td>读取之前要跳过的行数，默认为空</td>
</tr>
<tr class="even">
<td>n_max</td>
<td>读取的最大行数</td>
</tr>
<tr class="odd">
<td>guess_max</td>
<td>猜测列类型的最大行数,默认为1000</td>
</tr>
<tr class="even">
<td>progress</td>
<td>读取过程是否显示进度</td>
</tr>
<tr class="odd">
<td>.name_repair</td>
<td>自动处理列名，避免列名为空以及列名重复</td>
</tr>
</tbody>
</table>
<p>col_types 参数个人觉得比较重要，可以指定列的类型，可以避免一些错误。可用选项:“skip”, “guess”, “logical”, “numeric”, “date”, “text” or “list”。</p>
<p>.name_repair 参数能自动避免重复字段，可避免手工 Excel 出现字段名不唯一的情况。</p>
</div>
<div id="batch:read" class="section level3">
<h3>批量读取</h3>
<p>实际工作中，某文件夹下有相同格式的 Excel 文件(sheet名称以及列字段相同)，想要合并全部 Excel 数据，熟悉 Excel 的可以通过 Excel 的<code>power query</code>的获取数据完成合并，那么在R中该如何实现呢？</p>
<p>示例：</p>
<p>文件夹内 excel 文件</p>
<p>将文件夹『data/read-write』里以 a-z 开头的 xlsx 文件合并读取，R代码如下：</p>
<ul>
<li>base R</li>
</ul>
<p>不借助R包，使用 base R 实现</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>allfiles <span class="ot">&lt;-</span> <span class="fu">list.files</span>(<span class="at">path =</span> <span class="st">&#39;data/read-write/&#39;</span>,<span class="at">pattern =</span> <span class="st">&#39;^[a-z].xlsx$&#39;</span>,<span class="at">full.names =</span> <span class="cn">TRUE</span>)</span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a>datalist <span class="ot">&lt;-</span> <span class="fu">lapply</span>(allfiles,readxl<span class="sc">::</span>read_excel)</span>
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a><span class="fu">do.call</span>(rbind,datalist)</span></code></pre></div>
<ul>
<li>purrr</li>
</ul>
<p>使用 purrr 包合并读取</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>allfiles <span class="ot">&lt;-</span> <span class="fu">list.files</span>(<span class="at">path =</span> <span class="st">&#39;./data/read-write/&#39;</span>,<span class="at">pattern =</span> <span class="st">&#39;^[a-z].xlsx$&#39;</span>,<span class="at">full.names =</span> T)</span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a>purrr<span class="sc">::</span><span class="fu">map_dfr</span>(allfiles,read_excel)</span></code></pre></div>
<p><code>list.files()</code>函数按照规则返回某文件目录下的全部文件。关于R语言中文件系统操作可以查看章节<a href="#file:system-operation">R语言文件系统操作</a></p>
</div>
<div id="批量输出" class="section level3">
<h3>批量输出</h3>
<p>我们按照一定条件拆解数据集，分别输出，代码如下：</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(readxl)</span>
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></a>dt <span class="ot">&lt;-</span> <span class="fu">read_xlsx</span>(<span class="at">path =</span> <span class="st">&#39;./data/read-write/批量读写.xlsx&#39;</span>)</span>
<span id="cb14-5"><a href="#cb14-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-6"><a href="#cb14-6" aria-hidden="true" tabindex="-1"></a>dt <span class="sc">%&gt;%</span> </span>
<span id="cb14-7"><a href="#cb14-7" aria-hidden="true" tabindex="-1"></a>  <span class="fu">group_by</span>(name) <span class="sc">%&gt;%</span> </span>
<span id="cb14-8"><a href="#cb14-8" aria-hidden="true" tabindex="-1"></a>  <span class="fu">group_walk</span>(<span class="sc">~</span> <span class="fu">write.csv</span>(.x,<span class="at">file =</span> <span class="fu">file.path</span>(<span class="st">&#39;data/read-write&#39;</span>,<span class="fu">paste0</span>(.y<span class="sc">$</span>name,<span class="st">&#39;.csv&#39;</span>))))</span>
<span id="cb14-9"><a href="#cb14-9" aria-hidden="true" tabindex="-1"></a><span class="fu">list.files</span>(<span class="at">path =</span> <span class="st">&#39;data/read-write/&#39;</span>)</span></code></pre></div>
<blockquote>
<p>暂时不用理解批量读取和输出的代码具体含义，可以先记住用法。</p>
</blockquote>
</div>
</div>
<div id="writexl" class="section level2">
<h2>writexl</h2>
<p>截止到2021年5月17日，writexl包功能比较简单，仅有输出 Excel 功能。快速、不依赖 java 和 Excle 是它绝对的优势，并且输出文件相比<code>openxlsx</code>包较小。</p>
<p><a href="https://docs.ropensci.org/writexl/">项目地址</a></p>
<div id="writexl:usage" class="section level3">
<h3>用法</h3>
<p>1.安装</p>
<p>从CRAN安装发行版</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">&quot;writexl&quot;</span>)</span></code></pre></div>
<p>2.函数参数</p>
<p><code>write_xlsx()</code>函数参数如下：</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="fu">write_xlsx</span>(</span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a>  x,</span>
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a>  <span class="at">path =</span> <span class="fu">tempfile</span>(<span class="at">fileext =</span> <span class="st">&quot;.xlsx&quot;</span>),</span>
<span id="cb16-4"><a href="#cb16-4" aria-hidden="true" tabindex="-1"></a>  <span class="at">col_names =</span> <span class="cn">TRUE</span>,</span>
<span id="cb16-5"><a href="#cb16-5" aria-hidden="true" tabindex="-1"></a>  <span class="at">format_headers =</span> <span class="cn">TRUE</span>,</span>
<span id="cb16-6"><a href="#cb16-6" aria-hidden="true" tabindex="-1"></a>  <span class="at">use_zip64 =</span> <span class="cn">FALSE</span></span>
<span id="cb16-7"><a href="#cb16-7" aria-hidden="true" tabindex="-1"></a>)</span></code></pre></div>
<table>
<thead>
<tr class="header">
<th>write_xlsx参数</th>
<th>解释</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>x</td>
<td>要输出的数据框和数据框列表</td>
</tr>
<tr class="even">
<td>path</td>
<td>输出 Excel 的文件名称</td>
</tr>
<tr class="odd">
<td>col_names</td>
<td>是否输出表头，默认为TRUE</td>
</tr>
<tr class="even">
<td>format_headers</td>
<td>是否将表头居中并加粗，默认为TRUE</td>
</tr>
<tr class="odd">
<td>use_zip64</td>
<td>是否支持4Gb文件大小，不是所有的系统都支持</td>
</tr>
</tbody>
</table>
<blockquote>
<p>use_zip64:不确定该参数的理解是否正确，请自行阅读官方文档，绝大部分时候不用理解该参数。</p>
</blockquote>
<p>3.输出Excel</p>
<p><code>write_xlsx()</code>函数将数据框保存到 Excel。如想要输出多个 sheet 时，用<code>list(mysheet1 = iris,mysheet2 = iris)</code>表示即可，代码如下：</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(writexl)</span>
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a>writexl<span class="sc">::</span><span class="fu">write_xlsx</span>(iris,<span class="at">path =</span> <span class="st">&#39;iris.xlsx&#39;</span>)</span>
<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb17-4"><a href="#cb17-4" aria-hidden="true" tabindex="-1"></a><span class="co"># 输出多sheet</span></span>
<span id="cb17-5"><a href="#cb17-5" aria-hidden="true" tabindex="-1"></a><span class="fu">write_xlsx</span>(<span class="fu">list</span>(<span class="at">mysheet1 =</span> iris,<span class="at">mysheet2 =</span> iris),<span class="at">path =</span> <span class="st">&#39;iris.xlsx&#39;</span>)</span></code></pre></div>
<p>4.效率比较</p>
<p>和<a href="#openxlsx:description"><code>openxlsx</code></a>包比较输出效率。</p>
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(microbenchmark)</span>
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(nycflights13)</span>
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a><span class="fu">microbenchmark</span>(</span>
<span id="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a>  <span class="at">writexl =</span> writexl<span class="sc">::</span><span class="fu">write_xlsx</span>(flights, <span class="fu">tempfile</span>()),</span>
<span id="cb18-5"><a href="#cb18-5" aria-hidden="true" tabindex="-1"></a>  <span class="at">openxlsx =</span> openxlsx<span class="sc">::</span><span class="fu">write.xlsx</span>(flights, <span class="fu">tempfile</span>()),</span>
<span id="cb18-6"><a href="#cb18-6" aria-hidden="true" tabindex="-1"></a>  <span class="at">times =</span> <span class="dv">2</span></span>
<span id="cb18-7"><a href="#cb18-7" aria-hidden="true" tabindex="-1"></a>)</span></code></pre></div>
<p>5.文件大小比较</p>
<p>和<a href="#openxlsx:description"><code>openxlsx</code></a>包比较输出后的 Excel 文件大小。</p>
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(nycflights13)</span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a>writexl<span class="sc">::</span><span class="fu">write_xlsx</span>(flights, tmp1 <span class="ot">&lt;-</span> <span class="fu">tempfile</span>())</span>
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a><span class="fu">file.info</span>(tmp1)<span class="sc">$</span>size</span></code></pre></div>
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a>openxlsx<span class="sc">::</span><span class="fu">write.xlsx</span>(flights, tmp2 <span class="ot">&lt;-</span> <span class="fu">tempfile</span>())</span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a><span class="fu">file.info</span>(tmp2)<span class="sc">$</span>size</span></code></pre></div>
<p>按照<code>writexl</code>官方文档的说法，<code>writexl</code>输出的 Excel 文件应该较少，但实际上并不是，不确定是不是<code>openxlsx</code>更新优化过。</p>
<p>6.其它功能</p>
<p>Excel 公式以及超级链接格式。</p>
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a>df <span class="ot">&lt;-</span> <span class="fu">data.frame</span>(</span>
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a>  <span class="at">name =</span> <span class="fu">c</span>(<span class="st">&quot;UCLA&quot;</span>, <span class="st">&quot;Berkeley&quot;</span>, <span class="st">&quot;Jeroen&quot;</span>),</span>
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a>  <span class="at">founded =</span> <span class="fu">c</span>(<span class="dv">1919</span>, <span class="dv">1868</span>, <span class="dv">2030</span>),</span>
<span id="cb21-4"><a href="#cb21-4" aria-hidden="true" tabindex="-1"></a>  <span class="at">website =</span> <span class="fu">xl_hyperlink</span>(<span class="fu">c</span>(<span class="st">&quot;http://www.ucla.edu&quot;</span>, <span class="st">&quot;http://www.berkeley.edu&quot;</span>, <span class="cn">NA</span>), <span class="st">&quot;homepage&quot;</span>)</span>
<span id="cb21-5"><a href="#cb21-5" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb21-6"><a href="#cb21-6" aria-hidden="true" tabindex="-1"></a>df<span class="sc">$</span>age <span class="ot">&lt;-</span> <span class="fu">xl_formula</span>(<span class="st">&#39;=(YEAR(TODAY()) - INDIRECT(&quot;B&quot; &amp; ROW()))&#39;</span>)</span>
<span id="cb21-7"><a href="#cb21-7" aria-hidden="true" tabindex="-1"></a><span class="fu">write_xlsx</span>(df, <span class="st">&#39;universities.xlsx&#39;</span>)</span>
<span id="cb21-8"><a href="#cb21-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb21-9"><a href="#cb21-9" aria-hidden="true" tabindex="-1"></a><span class="co"># cleanup</span></span>
<span id="cb21-10"><a href="#cb21-10" aria-hidden="true" tabindex="-1"></a><span class="fu">unlink</span>(<span class="st">&#39;universities.xlsx&#39;</span>)</span></code></pre></div>
<p>当需要输出大型 Excel 文件时，比如超过50万行，我会采用<code>writexl</code>包输出。但是在曾经使用过程中，定时任务采用 writexl 包输出 Excel 文件有时会报错，感觉不稳定，原因不明。现在一般临时性的输出我会采用<code>writexl</code>包，定时任务中采用<a href="#openxlsx:description"><code>openxlsx</code></a></p>
</div>
</div>
<div id="openxlsx:description" class="section level2">
<h2>openxlsx</h2>
<p>openxlsx 包是当我需要定制输出 Excel 表格或报表时常用R包。目前该包的版本4.2.3，通过使用 Rcpp 加速，包的读写速度在 Excel 的百万级下是可接受状态，包的相关函数功能完善且简易好用，并且正在积极开发中，相信它以后功能会越来越强大。</p>
<p>项目官方地址:<a href="https://ycphs.github.io/openxlsx/index.html" class="uri">https://ycphs.github.io/openxlsx/index.html</a></p>
<p>openxlsx 主要优势：</p>
<ul>
<li>不依赖 java 环境</li>
<li>读写速度可接受</li>
<li>可设置条件格式，与 Excel 中『开始』选项卡的条件格式功能接近</li>
<li>可批量插入 ggplot2 图</li>
<li>可插入公式</li>
<li>可渲染大部分 Excel 格式，并且效率相比部分 python 包高效</li>
<li>可添加页眉页脚以及其他格式，方便直接打印</li>
<li>功能稳定可用并且在积极开发中</li>
</ul>
<p>版本信息查看</p>
<div class="sourceCode" id="cb22"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="fu">packageVersion</span>(<span class="st">&quot;openxlsx&quot;</span>)</span></code></pre></div>
<p>关于 openxlsx 更加详细的阐述:<a href="https://mp.weixin.qq.com/s/ZD0dJb0y8fsWGI1dCPh2mQ" class="uri">https://mp.weixin.qq.com/s/ZD0dJb0y8fsWGI1dCPh2mQ</a></p>
<div id="openxlsx:install" class="section level3">
<h3>安装</h3>
<p>从CRAN安装发行版</p>
<div class="sourceCode" id="cb23"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 稳定版</span></span>
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">&quot;openxlsx&quot;</span>, <span class="at">dependencies =</span> <span class="cn">TRUE</span>, <span class="at">repos =</span> <span class="st">&quot;https://mirrors.tuna.tsinghua.edu.cn/CRAN/&quot;</span>)</span></code></pre></div>
<p>从Github安装开发版</p>
<div class="sourceCode" id="cb24"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="fu">c</span>(<span class="st">&quot;Rcpp&quot;</span>, <span class="st">&quot;devtools&quot;</span>), <span class="at">dependencies =</span> <span class="cn">TRUE</span>)</span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(devtools)</span>
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a><span class="fu">install_github</span>(<span class="st">&quot;ycphs/openxlsx&quot;</span>)</span></code></pre></div>
</div>
<div id="openxlsx:functions" class="section level3">
<h3>基础功能</h3>
<p>本文仅呈现基础功能部分，即读写 Excel 文件。其它功能，请查阅项目官方地址或微信公众号文章<a href="https://mp.weixin.qq.com/s/ZD0dJb0y8fsWGI1dCPh2mQ">R包-openxlsx-学习笔记</a></p>
<div id="openxlsx:read-function" class="section level4">
<h4>读取 Excel</h4>
<p><code>read.xlsx()</code>函数读取 Excel 文件。</p>
<div class="sourceCode" id="cb25"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(openxlsx) </span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a><span class="fu">read.xlsx</span>(<span class="st">&#39;./data/read-write/批量读写.xlsx&#39;</span>)</span></code></pre></div>
<p><code>read.xlsx()</code>参数如下：</p>
<div class="sourceCode" id="cb26"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(openxlsx)</span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a><span class="fu">read.xlsx</span>(</span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a>  xlsxFile,</span>
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a>  <span class="at">sheet =</span> <span class="dv">1</span>,</span>
<span id="cb26-5"><a href="#cb26-5" aria-hidden="true" tabindex="-1"></a>  <span class="at">startRow =</span> <span class="dv">1</span>,</span>
<span id="cb26-6"><a href="#cb26-6" aria-hidden="true" tabindex="-1"></a>  <span class="at">colNames =</span> <span class="cn">TRUE</span>,</span>
<span id="cb26-7"><a href="#cb26-7" aria-hidden="true" tabindex="-1"></a>  <span class="at">rowNames =</span> <span class="cn">FALSE</span>,</span>
<span id="cb26-8"><a href="#cb26-8" aria-hidden="true" tabindex="-1"></a>  <span class="at">detectDates =</span> <span class="cn">FALSE</span>,</span>
<span id="cb26-9"><a href="#cb26-9" aria-hidden="true" tabindex="-1"></a>  <span class="at">skipEmptyRows =</span> <span class="cn">TRUE</span>,</span>
<span id="cb26-10"><a href="#cb26-10" aria-hidden="true" tabindex="-1"></a>  <span class="at">skipEmptyCols =</span> <span class="cn">TRUE</span>,</span>
<span id="cb26-11"><a href="#cb26-11" aria-hidden="true" tabindex="-1"></a>  <span class="at">rows =</span> <span class="cn">NULL</span>,</span>
<span id="cb26-12"><a href="#cb26-12" aria-hidden="true" tabindex="-1"></a>  <span class="at">cols =</span> <span class="cn">NULL</span>,</span>
<span id="cb26-13"><a href="#cb26-13" aria-hidden="true" tabindex="-1"></a>  <span class="at">check.names =</span> <span class="cn">FALSE</span>,</span>
<span id="cb26-14"><a href="#cb26-14" aria-hidden="true" tabindex="-1"></a>  <span class="at">sep.names =</span> <span class="st">&quot;.&quot;</span>,</span>
<span id="cb26-15"><a href="#cb26-15" aria-hidden="true" tabindex="-1"></a>  <span class="at">namedRegion =</span> <span class="cn">NULL</span>,</span>
<span id="cb26-16"><a href="#cb26-16" aria-hidden="true" tabindex="-1"></a>  <span class="at">na.strings =</span> <span class="st">&quot;NA&quot;</span>,</span>
<span id="cb26-17"><a href="#cb26-17" aria-hidden="true" tabindex="-1"></a>  <span class="at">fillMergedCells =</span> <span class="cn">FALSE</span></span>
<span id="cb26-18"><a href="#cb26-18" aria-hidden="true" tabindex="-1"></a>)</span></code></pre></div>
<p>以上参数中需要注意：detecDates 参数，当你的 Excel 表格中带日期列时需要将参数设置为TRUE,不然将会把日期识别为数字读入。</p>
<p>fillMergedCells参数，当你读取的表格中存在合并单元格，将用值填充其他全部单元格,如下所示：</p>
<div class="figure">
<img src="https://gitee.com/zhongyufei/photo-bed/raw/pic/img/merge-cell-xlsx.png" alt="" />
<p class="caption">merge-cell</p>
</div>
<div class="sourceCode" id="cb27"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="fu">read.xlsx</span>(<span class="st">&#39;./test.xlsx&#39;</span>,<span class="at">detectDates =</span> <span class="cn">TRUE</span>,<span class="at">fillMergedCells =</span> <span class="cn">TRUE</span>)</span></code></pre></div>
<p>读取后如下所示：</p>
<div class="figure">
<img src="https://gitee.com/zhongyufei/photo-bed/raw/pic/img/R-read-merge-xlsx.png" alt="" />
<p class="caption">openxlsx-merge-xlsx</p>
</div>
<p>readWorkbook()也可以读取 Excel 表格数据，参数与read.xlsx基本一致。</p>
<div class="sourceCode" id="cb28"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a>xlsxFile <span class="ot">&lt;-</span> <span class="fu">system.file</span>(<span class="st">&quot;extdata&quot;</span>, <span class="st">&quot;readTest.xlsx&quot;</span>, <span class="at">package =</span> <span class="st">&quot;openxlsx&quot;</span>)</span>
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a>df1 <span class="ot">&lt;-</span> <span class="fu">readWorkbook</span>(<span class="at">xlsxFile =</span> xlsxFile, <span class="at">sheet =</span> <span class="dv">1</span>)</span></code></pre></div>
</div>
<div id="openxlsx:write-function" class="section level4">
<h4>写入Excel</h4>
<p>数据清洗完后，或者是分组聚合完成，需要将结果从R导出到Excle，这时就利用<code>write.xlsx()</code>函数将结果数据集写入 Excel 中。</p>
<p>write.xlsx()函数写入</p>
<div class="sourceCode" id="cb29"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="fu">write.xlsx</span>(iris, <span class="at">file =</span> <span class="st">&quot;writeXLSX1.xlsx&quot;</span>, <span class="at">colNames =</span> <span class="cn">TRUE</span>, <span class="at">borders =</span> <span class="st">&quot;columns&quot;</span>)</span></code></pre></div>
<p>带格式输出</p>
<div class="sourceCode" id="cb30"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a>hs <span class="ot">&lt;-</span> <span class="fu">createStyle</span>(</span>
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a>  <span class="at">textDecoration =</span> <span class="st">&quot;BOLD&quot;</span>, <span class="at">fontColour =</span> <span class="st">&quot;#FFFFFF&quot;</span>, <span class="at">fontSize =</span> <span class="dv">12</span>,</span>
<span id="cb30-3"><a href="#cb30-3" aria-hidden="true" tabindex="-1"></a>  <span class="at">fontName =</span> <span class="st">&quot;Arial Narrow&quot;</span>, <span class="at">fgFill =</span> <span class="st">&quot;#4F80BD&quot;</span></span>
<span id="cb30-4"><a href="#cb30-4" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb30-5"><a href="#cb30-5" aria-hidden="true" tabindex="-1"></a><span class="do">## Not run: </span></span>
<span id="cb30-6"><a href="#cb30-6" aria-hidden="true" tabindex="-1"></a><span class="fu">write.xlsx</span>(iris,</span>
<span id="cb30-7"><a href="#cb30-7" aria-hidden="true" tabindex="-1"></a>  <span class="at">file =</span> <span class="st">&quot;writeXLSX3.xlsx&quot;</span>,</span>
<span id="cb30-8"><a href="#cb30-8" aria-hidden="true" tabindex="-1"></a>  <span class="at">colNames =</span> <span class="cn">TRUE</span>, <span class="at">borders =</span> <span class="st">&quot;rows&quot;</span>, <span class="at">headerStyle =</span> hs</span>
<span id="cb30-9"><a href="#cb30-9" aria-hidden="true" tabindex="-1"></a>)</span></code></pre></div>
<p><a href="https://www.sioe.cn/yingyong/yanse-rgb-16/">#4F80BD</a>格式属于十六进制颜色码。</p>
</div>
</div>
<div id="定制格式输出" class="section level3">
<h3>定制格式输出</h3>
<p>openxlsx包有两套输出函数，<code>wrire.xlsx()</code>是其中一种，还有如下一种。</p>
<p>输出过程共分为四步，第一步创建workbook,第二步添加sheet,第三步写入数据，第四步保存workbook。在输出的过程中可以通过<code>addStyle()</code>、<code>createStyle()</code>或<code>conditionalFormatting</code>添加格式或条件格式。</p>
<div class="sourceCode" id="cb31"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a>df <span class="ot">&lt;-</span> <span class="fu">data.frame</span>(<span class="at">a=</span><span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>,<span class="at">b=</span><span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>,<span class="at">d=</span><span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>)</span>
<span id="cb31-2"><a href="#cb31-2" aria-hidden="true" tabindex="-1"></a>wb <span class="ot">&lt;-</span> <span class="fu">createWorkbook</span>(<span class="at">creator =</span> <span class="st">&#39;zhongyf&#39;</span>,<span class="at">title =</span> <span class="st">&#39;test&#39;</span>)</span>
<span id="cb31-3"><a href="#cb31-3" aria-hidden="true" tabindex="-1"></a><span class="fu">addWorksheet</span>(wb,<span class="at">sheetName =</span> <span class="st">&#39;test&#39;</span>)</span>
<span id="cb31-4"><a href="#cb31-4" aria-hidden="true" tabindex="-1"></a><span class="fu">writeDataTable</span>(wb,<span class="at">sheet =</span> <span class="st">&#39;test&#39;</span>,<span class="at">x =</span> df)</span>
<span id="cb31-5"><a href="#cb31-5" aria-hidden="true" tabindex="-1"></a><span class="fu">saveWorkbook</span>(wb, <span class="st">&quot;test.xlsx&quot;</span>, <span class="at">overwrite =</span> <span class="cn">TRUE</span>)</span></code></pre></div>
<p>我们以上面四步输出的方式，查看包自带的例子。</p>
<ul>
<li><p>createWorkbook()</p></li>
<li><p>addWorksheet()</p></li>
<li><p>writeDataTable()</p></li>
<li><p>saveWorkbook()</p></li>
</ul>
<div class="sourceCode" id="cb32"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a>wb <span class="ot">&lt;-</span> <span class="fu">createWorkbook</span>(<span class="st">&quot;Fred&quot;</span>)</span>
<span id="cb32-2"><a href="#cb32-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb32-3"><a href="#cb32-3" aria-hidden="true" tabindex="-1"></a><span class="do">## Add 3 worksheets</span></span>
<span id="cb32-4"><a href="#cb32-4" aria-hidden="true" tabindex="-1"></a><span class="fu">addWorksheet</span>(wb, <span class="st">&quot;Sheet 1&quot;</span>)</span>
<span id="cb32-5"><a href="#cb32-5" aria-hidden="true" tabindex="-1"></a><span class="fu">addWorksheet</span>(wb, <span class="st">&quot;Sheet 2&quot;</span>, <span class="at">gridLines =</span> <span class="cn">FALSE</span>)</span>
<span id="cb32-6"><a href="#cb32-6" aria-hidden="true" tabindex="-1"></a><span class="fu">addWorksheet</span>(wb, <span class="st">&quot;Sheet 3&quot;</span>, <span class="at">tabColour =</span> <span class="st">&quot;red&quot;</span>)</span>
<span id="cb32-7"><a href="#cb32-7" aria-hidden="true" tabindex="-1"></a><span class="fu">addWorksheet</span>(wb, <span class="st">&quot;Sheet 4&quot;</span>, <span class="at">gridLines =</span> <span class="cn">FALSE</span>, <span class="at">tabColour =</span> <span class="st">&quot;#4F81BD&quot;</span>)</span>
<span id="cb32-8"><a href="#cb32-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb32-9"><a href="#cb32-9" aria-hidden="true" tabindex="-1"></a><span class="do">## Headers and Footers</span></span>
<span id="cb32-10"><a href="#cb32-10" aria-hidden="true" tabindex="-1"></a><span class="fu">addWorksheet</span>(wb, <span class="st">&quot;Sheet 5&quot;</span>,</span>
<span id="cb32-11"><a href="#cb32-11" aria-hidden="true" tabindex="-1"></a>  <span class="at">header =</span> <span class="fu">c</span>(<span class="st">&quot;ODD HEAD LEFT&quot;</span>, <span class="st">&quot;ODD HEAD CENTER&quot;</span>, <span class="st">&quot;ODD HEAD RIGHT&quot;</span>),</span>
<span id="cb32-12"><a href="#cb32-12" aria-hidden="true" tabindex="-1"></a>  <span class="at">footer =</span> <span class="fu">c</span>(<span class="st">&quot;ODD FOOT RIGHT&quot;</span>, <span class="st">&quot;ODD FOOT CENTER&quot;</span>, <span class="st">&quot;ODD FOOT RIGHT&quot;</span>),</span>
<span id="cb32-13"><a href="#cb32-13" aria-hidden="true" tabindex="-1"></a>  <span class="at">evenHeader =</span> <span class="fu">c</span>(<span class="st">&quot;EVEN HEAD LEFT&quot;</span>, <span class="st">&quot;EVEN HEAD CENTER&quot;</span>, <span class="st">&quot;EVEN HEAD RIGHT&quot;</span>),</span>
<span id="cb32-14"><a href="#cb32-14" aria-hidden="true" tabindex="-1"></a>  <span class="at">evenFooter =</span> <span class="fu">c</span>(<span class="st">&quot;EVEN FOOT RIGHT&quot;</span>, <span class="st">&quot;EVEN FOOT CENTER&quot;</span>, <span class="st">&quot;EVEN FOOT RIGHT&quot;</span>),</span>
<span id="cb32-15"><a href="#cb32-15" aria-hidden="true" tabindex="-1"></a>  <span class="at">firstHeader =</span> <span class="fu">c</span>(<span class="st">&quot;TOP&quot;</span>, <span class="st">&quot;OF FIRST&quot;</span>, <span class="st">&quot;PAGE&quot;</span>),</span>
<span id="cb32-16"><a href="#cb32-16" aria-hidden="true" tabindex="-1"></a>  <span class="at">firstFooter =</span> <span class="fu">c</span>(<span class="st">&quot;BOTTOM&quot;</span>, <span class="st">&quot;OF FIRST&quot;</span>, <span class="st">&quot;PAGE&quot;</span>)</span>
<span id="cb32-17"><a href="#cb32-17" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb32-18"><a href="#cb32-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb32-19"><a href="#cb32-19" aria-hidden="true" tabindex="-1"></a><span class="fu">addWorksheet</span>(wb, <span class="st">&quot;Sheet 6&quot;</span>,</span>
<span id="cb32-20"><a href="#cb32-20" aria-hidden="true" tabindex="-1"></a>  <span class="at">header =</span> <span class="fu">c</span>(<span class="st">&quot;&amp;[Date]&quot;</span>, <span class="st">&quot;ALL HEAD CENTER 2&quot;</span>, <span class="st">&quot;&amp;[Page] / &amp;[Pages]&quot;</span>),</span>
<span id="cb32-21"><a href="#cb32-21" aria-hidden="true" tabindex="-1"></a>  <span class="at">footer =</span> <span class="fu">c</span>(<span class="st">&quot;&amp;[Path]&amp;[File]&quot;</span>, <span class="cn">NA</span>, <span class="st">&quot;&amp;[Tab]&quot;</span>),</span>
<span id="cb32-22"><a href="#cb32-22" aria-hidden="true" tabindex="-1"></a>  <span class="at">firstHeader =</span> <span class="fu">c</span>(<span class="cn">NA</span>, <span class="st">&quot;Center Header of First Page&quot;</span>, <span class="cn">NA</span>),</span>
<span id="cb32-23"><a href="#cb32-23" aria-hidden="true" tabindex="-1"></a>  <span class="at">firstFooter =</span> <span class="fu">c</span>(<span class="cn">NA</span>, <span class="st">&quot;Center Footer of First Page&quot;</span>, <span class="cn">NA</span>)</span>
<span id="cb32-24"><a href="#cb32-24" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb32-25"><a href="#cb32-25" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb32-26"><a href="#cb32-26" aria-hidden="true" tabindex="-1"></a><span class="fu">addWorksheet</span>(wb, <span class="st">&quot;Sheet 7&quot;</span>,</span>
<span id="cb32-27"><a href="#cb32-27" aria-hidden="true" tabindex="-1"></a>  <span class="at">header =</span> <span class="fu">c</span>(<span class="st">&quot;ALL HEAD LEFT 2&quot;</span>, <span class="st">&quot;ALL HEAD CENTER 2&quot;</span>, <span class="st">&quot;ALL HEAD RIGHT 2&quot;</span>),</span>
<span id="cb32-28"><a href="#cb32-28" aria-hidden="true" tabindex="-1"></a>  <span class="at">footer =</span> <span class="fu">c</span>(<span class="st">&quot;ALL FOOT RIGHT 2&quot;</span>, <span class="st">&quot;ALL FOOT CENTER 2&quot;</span>, <span class="st">&quot;ALL FOOT RIGHT 2&quot;</span>)</span>
<span id="cb32-29"><a href="#cb32-29" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb32-30"><a href="#cb32-30" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb32-31"><a href="#cb32-31" aria-hidden="true" tabindex="-1"></a><span class="fu">addWorksheet</span>(wb, <span class="st">&quot;Sheet 8&quot;</span>,</span>
<span id="cb32-32"><a href="#cb32-32" aria-hidden="true" tabindex="-1"></a>  <span class="at">firstHeader =</span> <span class="fu">c</span>(<span class="st">&quot;FIRST ONLY L&quot;</span>, <span class="cn">NA</span>, <span class="st">&quot;FIRST ONLY R&quot;</span>),</span>
<span id="cb32-33"><a href="#cb32-33" aria-hidden="true" tabindex="-1"></a>  <span class="at">firstFooter =</span> <span class="fu">c</span>(<span class="st">&quot;FIRST ONLY L&quot;</span>, <span class="cn">NA</span>, <span class="st">&quot;FIRST ONLY R&quot;</span>)</span>
<span id="cb32-34"><a href="#cb32-34" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb32-35"><a href="#cb32-35" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb32-36"><a href="#cb32-36" aria-hidden="true" tabindex="-1"></a><span class="fu">addWorksheet</span>(wb,<span class="st">&#39;dataframe&#39;</span>)</span>
<span id="cb32-37"><a href="#cb32-37" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb32-38"><a href="#cb32-38" aria-hidden="true" tabindex="-1"></a><span class="do">## Need data on worksheet to see all headers and footers</span></span>
<span id="cb32-39"><a href="#cb32-39" aria-hidden="true" tabindex="-1"></a><span class="fu">writeData</span>(wb, <span class="at">sheet =</span> <span class="dv">5</span>, <span class="dv">1</span><span class="sc">:</span><span class="dv">400</span>)</span>
<span id="cb32-40"><a href="#cb32-40" aria-hidden="true" tabindex="-1"></a><span class="fu">writeData</span>(wb, <span class="at">sheet =</span> <span class="dv">6</span>, <span class="dv">1</span><span class="sc">:</span><span class="dv">400</span>)</span>
<span id="cb32-41"><a href="#cb32-41" aria-hidden="true" tabindex="-1"></a><span class="fu">writeData</span>(wb, <span class="at">sheet =</span> <span class="dv">7</span>, <span class="dv">1</span><span class="sc">:</span><span class="dv">400</span>)</span>
<span id="cb32-42"><a href="#cb32-42" aria-hidden="true" tabindex="-1"></a><span class="fu">writeData</span>(wb, <span class="at">sheet =</span> <span class="dv">8</span>, <span class="dv">1</span><span class="sc">:</span><span class="dv">400</span>)</span>
<span id="cb32-43"><a href="#cb32-43" aria-hidden="true" tabindex="-1"></a><span class="fu">writeDataTable</span>(wb,<span class="at">sheet =</span> <span class="st">&#39;dataframe&#39;</span>,iris)</span>
<span id="cb32-44"><a href="#cb32-44" aria-hidden="true" tabindex="-1"></a><span class="do">## Save workbook</span></span>
<span id="cb32-45"><a href="#cb32-45" aria-hidden="true" tabindex="-1"></a><span class="do">## Not run: </span></span>
<span id="cb32-46"><a href="#cb32-46" aria-hidden="true" tabindex="-1"></a><span class="fu">saveWorkbook</span>(wb, <span class="st">&quot;addWorksheetExample.xlsx&quot;</span>, <span class="at">overwrite =</span> <span class="cn">TRUE</span>)</span></code></pre></div>
</div>
<div id="函数参数" class="section level3">
<h3>函数参数</h3>
<p>本小节主要讲<code>createWorkbook()</code>，<code>addWorksheet()</code>,<code>writeDataTable()</code>,<code>saveWorkbook()</code>四个函数的参数以及用法。</p>
<ul>
<li>createWorkbook</li>
</ul>
<div class="sourceCode" id="cb33"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a><span class="fu">createWorkbook</span>(</span>
<span id="cb33-2"><a href="#cb33-2" aria-hidden="true" tabindex="-1"></a>  <span class="at">creator =</span> <span class="fu">ifelse</span>(.Platform<span class="sc">$</span>OS.type <span class="sc">==</span> <span class="st">&quot;windows&quot;</span>, <span class="fu">Sys.getenv</span>(<span class="st">&quot;USERNAME&quot;</span>),</span>
<span id="cb33-3"><a href="#cb33-3" aria-hidden="true" tabindex="-1"></a>    <span class="fu">Sys.getenv</span>(<span class="st">&quot;USER&quot;</span>)),</span>
<span id="cb33-4"><a href="#cb33-4" aria-hidden="true" tabindex="-1"></a>  <span class="at">title =</span> <span class="cn">NULL</span>,</span>
<span id="cb33-5"><a href="#cb33-5" aria-hidden="true" tabindex="-1"></a>  <span class="at">subject =</span> <span class="cn">NULL</span>,</span>
<span id="cb33-6"><a href="#cb33-6" aria-hidden="true" tabindex="-1"></a>  <span class="at">category =</span> <span class="cn">NULL</span></span>
<span id="cb33-7"><a href="#cb33-7" aria-hidden="true" tabindex="-1"></a>)</span></code></pre></div>
<div class="sourceCode" id="cb34"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a>wb <span class="ot">&lt;-</span> <span class="fu">createWorkbook</span>(</span>
<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a>  <span class="at">creator =</span> <span class="st">&quot;宇飞的世界&quot;</span>,</span>
<span id="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></a>  <span class="at">title =</span> <span class="st">&quot;标题&quot;</span>,</span>
<span id="cb34-4"><a href="#cb34-4" aria-hidden="true" tabindex="-1"></a>  <span class="at">subject =</span> <span class="st">&quot;主题&quot;</span>,</span>
<span id="cb34-5"><a href="#cb34-5" aria-hidden="true" tabindex="-1"></a>  <span class="at">category =</span> <span class="st">&quot;类别目录&quot;</span></span>
<span id="cb34-6"><a href="#cb34-6" aria-hidden="true" tabindex="-1"></a>)</span></code></pre></div>
<p>在win系统上右击 Excel 文件属性，点击详细信息：</p>
<div class="figure">
<img src="picture/read-write/open-wb-attribute.png" alt="" />
<p class="caption">win-excel-attribute</p>
</div>
<ul>
<li>addWorksheet</li>
</ul>
<p><code>addWorksheet()</code>函数添加sheet。</p>
<div class="sourceCode" id="cb35"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a><span class="fu">addWorksheet</span>(</span>
<span id="cb35-2"><a href="#cb35-2" aria-hidden="true" tabindex="-1"></a>  wb,</span>
<span id="cb35-3"><a href="#cb35-3" aria-hidden="true" tabindex="-1"></a>  sheetName,</span>
<span id="cb35-4"><a href="#cb35-4" aria-hidden="true" tabindex="-1"></a>  <span class="at">gridLines =</span> <span class="cn">TRUE</span>,</span>
<span id="cb35-5"><a href="#cb35-5" aria-hidden="true" tabindex="-1"></a>  <span class="at">tabColour =</span> <span class="cn">NULL</span>,</span>
<span id="cb35-6"><a href="#cb35-6" aria-hidden="true" tabindex="-1"></a>  <span class="at">zoom =</span> <span class="dv">100</span>,</span>
<span id="cb35-7"><a href="#cb35-7" aria-hidden="true" tabindex="-1"></a>  <span class="at">header =</span> <span class="cn">NULL</span>,</span>
<span id="cb35-8"><a href="#cb35-8" aria-hidden="true" tabindex="-1"></a>  <span class="at">footer =</span> <span class="cn">NULL</span>,</span>
<span id="cb35-9"><a href="#cb35-9" aria-hidden="true" tabindex="-1"></a>  <span class="at">evenHeader =</span> <span class="cn">NULL</span>,</span>
<span id="cb35-10"><a href="#cb35-10" aria-hidden="true" tabindex="-1"></a>  <span class="at">evenFooter =</span> <span class="cn">NULL</span>,</span>
<span id="cb35-11"><a href="#cb35-11" aria-hidden="true" tabindex="-1"></a>  <span class="at">firstHeader =</span> <span class="cn">NULL</span>,</span>
<span id="cb35-12"><a href="#cb35-12" aria-hidden="true" tabindex="-1"></a>  <span class="at">firstFooter =</span> <span class="cn">NULL</span>,</span>
<span id="cb35-13"><a href="#cb35-13" aria-hidden="true" tabindex="-1"></a>  <span class="at">visible =</span> <span class="cn">TRUE</span>,</span>
<span id="cb35-14"><a href="#cb35-14" aria-hidden="true" tabindex="-1"></a>  <span class="at">paperSize =</span> <span class="fu">getOption</span>(<span class="st">&quot;openxlsx.paperSize&quot;</span>, <span class="at">default =</span> <span class="dv">9</span>),</span>
<span id="cb35-15"><a href="#cb35-15" aria-hidden="true" tabindex="-1"></a>  <span class="at">orientation =</span> <span class="fu">getOption</span>(<span class="st">&quot;openxlsx.orientation&quot;</span>, <span class="at">default =</span> <span class="st">&quot;portrait&quot;</span>),</span>
<span id="cb35-16"><a href="#cb35-16" aria-hidden="true" tabindex="-1"></a>  <span class="at">vdpi =</span> <span class="fu">getOption</span>(<span class="st">&quot;openxlsx.vdpi&quot;</span>, <span class="at">default =</span> <span class="fu">getOption</span>(<span class="st">&quot;openxlsx.dpi&quot;</span>, <span class="at">default =</span> <span class="dv">300</span>)),</span>
<span id="cb35-17"><a href="#cb35-17" aria-hidden="true" tabindex="-1"></a>  <span class="at">hdpi =</span> <span class="fu">getOption</span>(<span class="st">&quot;openxlsx.hdpi&quot;</span>, <span class="at">default =</span> <span class="fu">getOption</span>(<span class="st">&quot;openxlsx.dpi&quot;</span>, <span class="at">default =</span> <span class="dv">300</span>))</span>
<span id="cb35-18"><a href="#cb35-18" aria-hidden="true" tabindex="-1"></a>)</span></code></pre></div>
<p><code>addWorksheet()</code>参数解释如下：</p>
<div class="sourceCode" id="cb36"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a>gridLines参数：表格中是否有网格线，在Excle『视图』选项卡下面的网格线去除打勾的效果一致</span>
<span id="cb36-2"><a href="#cb36-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-3"><a href="#cb36-3" aria-hidden="true" tabindex="-1"></a>tabColour参数：输出表格sheet标签颜色</span>
<span id="cb36-4"><a href="#cb36-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-5"><a href="#cb36-5" aria-hidden="true" tabindex="-1"></a>zoom：发大缩小，默认是100，可选范围10<span class="dv">-400</span></span>
<span id="cb36-6"><a href="#cb36-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-7"><a href="#cb36-7" aria-hidden="true" tabindex="-1"></a>header<span class="sc">:</span>页眉 长度为3的字符向量，左、中、右三个位置，用Na可跳过一位置，以下页眉页脚相同。</span>
<span id="cb36-8"><a href="#cb36-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-9"><a href="#cb36-9" aria-hidden="true" tabindex="-1"></a>footer<span class="sc">:</span> 页脚</span>
<span id="cb36-10"><a href="#cb36-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-11"><a href="#cb36-11" aria-hidden="true" tabindex="-1"></a>evenHeader<span class="sc">:</span> 每页页眉</span>
<span id="cb36-12"><a href="#cb36-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-13"><a href="#cb36-13" aria-hidden="true" tabindex="-1"></a>evenFooter<span class="sc">:</span> 每页页脚</span>
<span id="cb36-14"><a href="#cb36-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-15"><a href="#cb36-15" aria-hidden="true" tabindex="-1"></a>firstHeader<span class="sc">:</span> 第一页页眉</span>
<span id="cb36-16"><a href="#cb36-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-17"><a href="#cb36-17" aria-hidden="true" tabindex="-1"></a>firstFooter<span class="sc">:</span> 第一页页脚</span>
<span id="cb36-18"><a href="#cb36-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-19"><a href="#cb36-19" aria-hidden="true" tabindex="-1"></a>visible<span class="sc">:</span>sheet是否隐藏，如果为否sheet将被隐藏</span>
<span id="cb36-20"><a href="#cb36-20" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-21"><a href="#cb36-21" aria-hidden="true" tabindex="-1"></a>paperSize<span class="sc">:</span>页面大小,详见 ?pageSetup </span>
<span id="cb36-22"><a href="#cb36-22" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-23"><a href="#cb36-23" aria-hidden="true" tabindex="-1"></a>orientation<span class="sc">:</span>One of <span class="st">&quot;portrait&quot;</span> or <span class="st">&quot;landscape&quot;</span> 不清楚干嘛用</span>
<span id="cb36-24"><a href="#cb36-24" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-25"><a href="#cb36-25" aria-hidden="true" tabindex="-1"></a>vdpi<span class="sc">:</span> 屏幕分辨率 默认值即可，不用调整</span>
<span id="cb36-26"><a href="#cb36-26" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb36-27"><a href="#cb36-27" aria-hidden="true" tabindex="-1"></a>hdpi<span class="sc">:</span> 屏幕分辨率 默认值即可，不用调整</span></code></pre></div>
<ul>
<li>writeDataTable</li>
</ul>
<p>writeDataTable()函数将data.frame写入 Excel 。</p>
<div class="sourceCode" id="cb37"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a><span class="fu">writeDataTable</span>(</span>
<span id="cb37-2"><a href="#cb37-2" aria-hidden="true" tabindex="-1"></a>  wb,</span>
<span id="cb37-3"><a href="#cb37-3" aria-hidden="true" tabindex="-1"></a>  sheet,</span>
<span id="cb37-4"><a href="#cb37-4" aria-hidden="true" tabindex="-1"></a>  x,</span>
<span id="cb37-5"><a href="#cb37-5" aria-hidden="true" tabindex="-1"></a>  <span class="at">startCol =</span> <span class="dv">1</span>,</span>
<span id="cb37-6"><a href="#cb37-6" aria-hidden="true" tabindex="-1"></a>  <span class="at">startRow =</span> <span class="dv">1</span>,</span>
<span id="cb37-7"><a href="#cb37-7" aria-hidden="true" tabindex="-1"></a>  <span class="at">xy =</span> <span class="cn">NULL</span>,</span>
<span id="cb37-8"><a href="#cb37-8" aria-hidden="true" tabindex="-1"></a>  <span class="at">colNames =</span> <span class="cn">TRUE</span>,</span>
<span id="cb37-9"><a href="#cb37-9" aria-hidden="true" tabindex="-1"></a>  <span class="at">rowNames =</span> <span class="cn">FALSE</span>,</span>
<span id="cb37-10"><a href="#cb37-10" aria-hidden="true" tabindex="-1"></a>  <span class="at">tableStyle =</span> <span class="st">&quot;TableStyleLight9&quot;</span>,</span>
<span id="cb37-11"><a href="#cb37-11" aria-hidden="true" tabindex="-1"></a>  <span class="at">tableName =</span> <span class="cn">NULL</span>,</span>
<span id="cb37-12"><a href="#cb37-12" aria-hidden="true" tabindex="-1"></a>  <span class="at">headerStyle =</span> <span class="cn">NULL</span>,</span>
<span id="cb37-13"><a href="#cb37-13" aria-hidden="true" tabindex="-1"></a>  <span class="at">withFilter =</span> <span class="cn">TRUE</span>,</span>
<span id="cb37-14"><a href="#cb37-14" aria-hidden="true" tabindex="-1"></a>  <span class="at">keepNA =</span> <span class="cn">FALSE</span>,</span>
<span id="cb37-15"><a href="#cb37-15" aria-hidden="true" tabindex="-1"></a>  <span class="at">na.string =</span> <span class="cn">NULL</span>,</span>
<span id="cb37-16"><a href="#cb37-16" aria-hidden="true" tabindex="-1"></a>  <span class="at">sep =</span> <span class="st">&quot;, &quot;</span>,</span>
<span id="cb37-17"><a href="#cb37-17" aria-hidden="true" tabindex="-1"></a>  <span class="at">stack =</span> <span class="cn">FALSE</span>,</span>
<span id="cb37-18"><a href="#cb37-18" aria-hidden="true" tabindex="-1"></a>  <span class="at">firstColumn =</span> <span class="cn">FALSE</span>,</span>
<span id="cb37-19"><a href="#cb37-19" aria-hidden="true" tabindex="-1"></a>  <span class="at">lastColumn =</span> <span class="cn">FALSE</span>,</span>
<span id="cb37-20"><a href="#cb37-20" aria-hidden="true" tabindex="-1"></a>  <span class="at">bandedRows =</span> <span class="cn">TRUE</span>,</span>
<span id="cb37-21"><a href="#cb37-21" aria-hidden="true" tabindex="-1"></a>  <span class="at">bandedCols =</span> <span class="cn">FALSE</span></span>
<span id="cb37-22"><a href="#cb37-22" aria-hidden="true" tabindex="-1"></a>)</span></code></pre></div>
<p><code>writeDataTable()</code>参数解释如下：</p>
<div class="sourceCode" id="cb38"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a>wb<span class="sc">:</span>即<span class="fu">createWorkbook</span>()函数创建的workbook对象</span>
<span id="cb38-2"><a href="#cb38-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb38-3"><a href="#cb38-3" aria-hidden="true" tabindex="-1"></a>sheet<span class="sc">:</span>将输入写入的sheet名，由<span class="fu">addWorksheet</span>()函数创建</span>
<span id="cb38-4"><a href="#cb38-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb38-5"><a href="#cb38-5" aria-hidden="true" tabindex="-1"></a>x <span class="sc">:</span> a data.frame</span>
<span id="cb38-6"><a href="#cb38-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb38-7"><a href="#cb38-7" aria-hidden="true" tabindex="-1"></a>startCol<span class="sc">:</span> 从哪一列开始写入,接受数字</span>
<span id="cb38-8"><a href="#cb38-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb38-9"><a href="#cb38-9" aria-hidden="true" tabindex="-1"></a>startRow：从哪一行开始写入，接受数字</span>
<span id="cb38-10"><a href="#cb38-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb38-11"><a href="#cb38-11" aria-hidden="true" tabindex="-1"></a>xy<span class="sc">:</span> 指定开始行以及开始列的向量，xy <span class="ot">=</span> <span class="fu">c</span>(<span class="dv">2</span>,<span class="dv">3</span>)即从2列3行位置开始写入</span>
<span id="cb38-12"><a href="#cb38-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb38-13"><a href="#cb38-13" aria-hidden="true" tabindex="-1"></a>colNames<span class="sc">:</span> 如果TRUE,列名写入</span>
<span id="cb38-14"><a href="#cb38-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb38-15"><a href="#cb38-15" aria-hidden="true" tabindex="-1"></a>rowNames<span class="sc">:</span> 如果TRUE,行名写入</span>
<span id="cb38-16"><a href="#cb38-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb38-17"><a href="#cb38-17" aria-hidden="true" tabindex="-1"></a>tableStyle<span class="sc">:</span>一些表格样式，在中文版Excle『开始』选项卡下面的套用表格格式，可以使用预定义的表格样式</span>
<span id="cb38-18"><a href="#cb38-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb38-19"><a href="#cb38-19" aria-hidden="true" tabindex="-1"></a>tableName<span class="sc">:</span>workbook中的表中，表名必须是唯一的，熟悉Power Pivot的人应该很清晰“表”概念</span>
<span id="cb38-20"><a href="#cb38-20" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb38-21"><a href="#cb38-21" aria-hidden="true" tabindex="-1"></a>headerStyle<span class="sc">:</span>自定义列样式应用,用<span class="fu">createStyle</span>()函数创建</span>
<span id="cb38-22"><a href="#cb38-22" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb38-23"><a href="#cb38-23" aria-hidden="true" tabindex="-1"></a>withFilter<span class="sc">:</span> 在第一行是否有筛选</span>
<span id="cb38-24"><a href="#cb38-24" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb38-25"><a href="#cb38-25" aria-hidden="true" tabindex="-1"></a>keepNA<span class="sc">:</span>如果TRUE,NA值转化为<span class="co">#NA，如果否，变为空</span></span>
<span id="cb38-26"><a href="#cb38-26" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb38-27"><a href="#cb38-27" aria-hidden="true" tabindex="-1"></a>na.string <span class="sc">:</span>如果参数不是NULL,如果keepNA是TRUE,NA将会在 Excel 中转为该参数指定的字符</span>
<span id="cb38-28"><a href="#cb38-28" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb38-29"><a href="#cb38-29" aria-hidden="true" tabindex="-1"></a>sep：列表列的分隔符，默认为<span class="st">&quot;,&quot;</span></span>
<span id="cb38-30"><a href="#cb38-30" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb38-31"><a href="#cb38-31" aria-hidden="true" tabindex="-1"></a>stack <span class="sc">:</span>详情请参照函数使用</span>
<span id="cb38-32"><a href="#cb38-32" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb38-33"><a href="#cb38-33" aria-hidden="true" tabindex="-1"></a>firstColumn<span class="sc">:</span>logical. If <span class="cn">TRUE</span>, the first column is bold</span>
<span id="cb38-34"><a href="#cb38-34" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb38-35"><a href="#cb38-35" aria-hidden="true" tabindex="-1"></a>lastColumn <span class="sc">:</span>logical. If <span class="cn">TRUE</span>, the last column is bold</span>
<span id="cb38-36"><a href="#cb38-36" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb38-37"><a href="#cb38-37" aria-hidden="true" tabindex="-1"></a>bandedRows<span class="sc">:</span>logical. If <span class="cn">TRUE</span>, rows are colour banded 即行线,默认为TRUE</span>
<span id="cb38-38"><a href="#cb38-38" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb38-39"><a href="#cb38-39" aria-hidden="true" tabindex="-1"></a>bandedCols<span class="sc">:</span>logical. If <span class="cn">TRUE</span>, the columns are colour banded 即列线 ,默认为TRUE</span></code></pre></div>
<ul>
<li>saveWorkbook</li>
</ul>
<div class="sourceCode" id="cb39"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a><span class="fu">saveWorkbook</span>(wb, file, <span class="at">overwrite =</span> <span class="cn">FALSE</span>, <span class="at">returnValue =</span> <span class="cn">FALSE</span>)</span></code></pre></div>
<p>参数较为简单，wb即上文中的workbook对象，file即输出的文件名，overwrite即如果存在是否覆盖，returnValue如果设置为TRUE，返回TRUE代表保存成功</p>
</div>
<div id="总结" class="section level3">
<h3>总结</h3>
<p>openxlsx包功能较为强大，更多详细用法大家可自行探索，或关注我的语雀笔记，笔记会不定期持续更新。</p>
<p>R包openxlsx学习笔记:<a href="https://www.yuque.com/docs/share/7a768e6f-95e0-417c-a9b5-dfc8862dc6be?#" class="uri">https://www.yuque.com/docs/share/7a768e6f-95e0-417c-a9b5-dfc8862dc6be?#</a></p>
<p>语雀个人主页:<a href="https://www.yuque.com/zyufei" class="uri">https://www.yuque.com/zyufei</a></p>
</div>
</div>
<div id="readr:package" class="section level2">
<h2>readr</h2>
<p>readr提供了一种快速友好的方式读取矩形数据<a href="#fn3" class="footnote-ref" id="fnref3"><sup>3</sup></a>(如：csv,tsv,fwf)，且当读取大型数据集时默认有进度条显示。</p>
<p>如果对readr包不熟悉，可以直接阅读包作者，大神Hadley Wickham的书R for data science 中<a href="https://r4ds.had.co.nz/data-import.html">data import chapter</a>章节。</p>
<div id="readr:install" class="section level3">
<h3>安装</h3>
<p>由于readr包是tidyverse系列核心包，可以直接安装tidyverse使用，或者单独安装readr。</p>
<div class="sourceCode" id="cb40"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 安装整个tidyverse</span></span>
<span id="cb40-2"><a href="#cb40-2" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">&quot;tidyverse&quot;</span>)</span>
<span id="cb40-3"><a href="#cb40-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb40-4"><a href="#cb40-4" aria-hidden="true" tabindex="-1"></a><span class="co"># 仅安装readr</span></span>
<span id="cb40-5"><a href="#cb40-5" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">&quot;readr&quot;</span>)</span>
<span id="cb40-6"><a href="#cb40-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb40-7"><a href="#cb40-7" aria-hidden="true" tabindex="-1"></a><span class="co"># 从github安装开发版</span></span>
<span id="cb40-8"><a href="#cb40-8" aria-hidden="true" tabindex="-1"></a><span class="co"># install.packages(&quot;devtools&quot;)</span></span>
<span id="cb40-9"><a href="#cb40-9" aria-hidden="true" tabindex="-1"></a>devtools<span class="sc">::</span><span class="fu">install_github</span>(<span class="st">&quot;tidyverse/readr&quot;</span>)</span></code></pre></div>
</div>
<div id="readr:usage" class="section level3">
<h3>用法</h3>
<p>readr包是tidyverse系列的核心包，可以加载tidyverse使用。</p>
<div class="sourceCode" id="cb41"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb41-1"><a href="#cb41-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span>
<span id="cb41-2"><a href="#cb41-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 或者</span></span>
<span id="cb41-3"><a href="#cb41-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(readr)</span></code></pre></div>
<div id="主要函数" class="section level4">
<h4>主要函数</h4>
<p>readr包支持七种<code>read_</code>功能的文件格式。</p>
<p>— <code>read_csv()</code>:逗号分隔符文件
- <code>read_tsv()</code>:制表符分割文件
- <code>read_delim()</code>:规定分隔符文件
- <code>read_fwf()</code>:固定宽度文件
- <code>read_table()</code>:表格文件，列间用空格隔开
- <code>read_log()</code>:Web日志文件</p>
<p>在大多数情况下，我们常使用<code>read_csv()</code>，提供文件路径，将得到数据表。示例如下:</p>
<div class="sourceCode" id="cb42"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb42-1"><a href="#cb42-1" aria-hidden="true" tabindex="-1"></a>mtcars <span class="ot">&lt;-</span> <span class="fu">read_csv</span>(<span class="fu">readr_example</span>(<span class="st">&quot;mtcars.csv&quot;</span>))</span></code></pre></div>
<p>通过上述输出反馈，我们可以知道读进去的数据集每列类型。如果发现不对可以通过<code>col_types</code>参数修改。大多数情况下，我们并不需要指定列的类型，readr会自动猜测列类型。</p>
<div class="sourceCode" id="cb43"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a>mtcars <span class="ot">&lt;-</span> <span class="fu">read_csv</span>(<span class="fu">readr_example</span>(<span class="st">&quot;mtcars.csv&quot;</span>), <span class="at">col_types =</span> </span>
<span id="cb43-2"><a href="#cb43-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">cols</span>(</span>
<span id="cb43-3"><a href="#cb43-3" aria-hidden="true" tabindex="-1"></a>    <span class="at">mpg =</span> <span class="fu">col_double</span>(),</span>
<span id="cb43-4"><a href="#cb43-4" aria-hidden="true" tabindex="-1"></a>    <span class="at">cyl =</span> <span class="fu">col_integer</span>(),</span>
<span id="cb43-5"><a href="#cb43-5" aria-hidden="true" tabindex="-1"></a>    <span class="at">disp =</span> <span class="fu">col_double</span>(),</span>
<span id="cb43-6"><a href="#cb43-6" aria-hidden="true" tabindex="-1"></a>    <span class="at">hp =</span> <span class="fu">col_integer</span>(),</span>
<span id="cb43-7"><a href="#cb43-7" aria-hidden="true" tabindex="-1"></a>    <span class="at">drat =</span> <span class="fu">col_double</span>(),</span>
<span id="cb43-8"><a href="#cb43-8" aria-hidden="true" tabindex="-1"></a>    <span class="at">vs =</span> <span class="fu">col_integer</span>(),</span>
<span id="cb43-9"><a href="#cb43-9" aria-hidden="true" tabindex="-1"></a>    <span class="at">wt =</span> <span class="fu">col_double</span>(),</span>
<span id="cb43-10"><a href="#cb43-10" aria-hidden="true" tabindex="-1"></a>    <span class="at">qsec =</span> <span class="fu">col_double</span>(),</span>
<span id="cb43-11"><a href="#cb43-11" aria-hidden="true" tabindex="-1"></a>    <span class="at">am =</span> <span class="fu">col_integer</span>(),</span>
<span id="cb43-12"><a href="#cb43-12" aria-hidden="true" tabindex="-1"></a>    <span class="at">gear =</span> <span class="fu">col_integer</span>(),</span>
<span id="cb43-13"><a href="#cb43-13" aria-hidden="true" tabindex="-1"></a>    <span class="at">carb =</span> <span class="fu">col_integer</span>()</span>
<span id="cb43-14"><a href="#cb43-14" aria-hidden="true" tabindex="-1"></a>  )</span>
<span id="cb43-15"><a href="#cb43-15" aria-hidden="true" tabindex="-1"></a>)</span></code></pre></div>
</div>
<div id="参数" class="section level4">
<h4>参数</h4>
<p><code>read_csv()</code>的参数如下：</p>
<div class="sourceCode" id="cb44"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb44-1"><a href="#cb44-1" aria-hidden="true" tabindex="-1"></a><span class="fu">read_csv</span>(</span>
<span id="cb44-2"><a href="#cb44-2" aria-hidden="true" tabindex="-1"></a>  file,</span>
<span id="cb44-3"><a href="#cb44-3" aria-hidden="true" tabindex="-1"></a>  <span class="at">col_names =</span> <span class="cn">TRUE</span>,</span>
<span id="cb44-4"><a href="#cb44-4" aria-hidden="true" tabindex="-1"></a>  <span class="at">col_types =</span> <span class="cn">NULL</span>,</span>
<span id="cb44-5"><a href="#cb44-5" aria-hidden="true" tabindex="-1"></a>  <span class="at">locale =</span> <span class="fu">default_locale</span>(),</span>
<span id="cb44-6"><a href="#cb44-6" aria-hidden="true" tabindex="-1"></a>  <span class="at">na =</span> <span class="fu">c</span>(<span class="st">&quot;&quot;</span>, <span class="st">&quot;NA&quot;</span>),</span>
<span id="cb44-7"><a href="#cb44-7" aria-hidden="true" tabindex="-1"></a>  <span class="at">quoted_na =</span> <span class="cn">TRUE</span>,</span>
<span id="cb44-8"><a href="#cb44-8" aria-hidden="true" tabindex="-1"></a>  <span class="at">quote =</span> <span class="st">&quot;</span><span class="sc">\&quot;</span><span class="st">&quot;</span>,</span>
<span id="cb44-9"><a href="#cb44-9" aria-hidden="true" tabindex="-1"></a>  <span class="at">comment =</span> <span class="st">&quot;&quot;</span>,</span>
<span id="cb44-10"><a href="#cb44-10" aria-hidden="true" tabindex="-1"></a>  <span class="at">trim_ws =</span> <span class="cn">TRUE</span>,</span>
<span id="cb44-11"><a href="#cb44-11" aria-hidden="true" tabindex="-1"></a>  <span class="at">skip =</span> <span class="dv">0</span>,</span>
<span id="cb44-12"><a href="#cb44-12" aria-hidden="true" tabindex="-1"></a>  <span class="at">n_max =</span> <span class="cn">Inf</span>,</span>
<span id="cb44-13"><a href="#cb44-13" aria-hidden="true" tabindex="-1"></a>  <span class="at">guess_max =</span> <span class="fu">min</span>(<span class="dv">1000</span>, n_max),</span>
<span id="cb44-14"><a href="#cb44-14" aria-hidden="true" tabindex="-1"></a>  <span class="at">progress =</span> <span class="fu">show_progress</span>(),</span>
<span id="cb44-15"><a href="#cb44-15" aria-hidden="true" tabindex="-1"></a>  <span class="at">skip_empty_rows =</span> <span class="cn">TRUE</span></span>
<span id="cb44-16"><a href="#cb44-16" aria-hidden="true" tabindex="-1"></a>)</span></code></pre></div>
<p>通过对 readxl 包的了解，我们发现两个包的读写函数的部分参数是一致的。</p>
<p>col_types :指定列类型，可用项如下所示(含简写):
c = character，i = integer，n = number，d = double，l = logical，f = factor，D = date，T = date time，t = time，默认值为 guess。</p>
<p>locale:locale参数是 readr 包中很重要的一个参数，指定日期使用的月和日的名称，时区，字符编码，日期格式，数字的小数和点位数和分隔符。</p>
<p><code>locale()</code>的第一个参数是date_names，控制月份和日期的名称，指定最简单的方式<a href="https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes">ISO 639 language code</a></p>
<div class="sourceCode" id="cb45"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a><span class="fu">locale</span>(<span class="st">&#39;zh&#39;</span>) <span class="co"># 中文</span></span>
<span id="cb45-2"><a href="#cb45-2" aria-hidden="true" tabindex="-1"></a><span class="fu">locale</span>(<span class="st">&#39;ja&#39;</span>) <span class="co">#日本</span></span>
<span id="cb45-3"><a href="#cb45-3" aria-hidden="true" tabindex="-1"></a><span class="fu">locale</span>(<span class="st">&#39;ko&#39;</span>) <span class="co">#韩国</span></span></code></pre></div>
<p>编码和时区问题是我们常面临的问题， 不正确设定导致我们的数据读取错误。</p>
<div class="sourceCode" id="cb46"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb46-1"><a href="#cb46-1" aria-hidden="true" tabindex="-1"></a><span class="fu">read_csv</span>(<span class="fu">readr_example</span>(<span class="st">&quot;mtcars.csv&quot;</span>),<span class="at">locale =</span> <span class="fu">locale</span>(<span class="at">encoding =</span> <span class="st">&#39;UTF-8&#39;</span>,<span class="at">tz =</span> <span class="st">&#39;Asia/Shanghai&#39;</span>))</span></code></pre></div>
<p>关于locales,详细信息查看手册<code>vignette("locales")</code>。</p>
</div>
</div>
<div id="readr:write-function" class="section level3">
<h3>导出功能</h3>
<p>由于系统缘故，在win系统下可能面临编码问题。我用readr导出数据时一般采用<code>write_excel_csv()</code>功能导出，目前还未遇到乱码情况。</p>
<p>需要说明的是<code>write_</code>系列函数可以将输出文件压缩。</p>
<div class="sourceCode" id="cb47"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb47-1"><a href="#cb47-1" aria-hidden="true" tabindex="-1"></a><span class="fu">data</span>(storms, <span class="at">package =</span> <span class="st">&quot;dplyr&quot;</span>)</span>
<span id="cb47-2"><a href="#cb47-2" aria-hidden="true" tabindex="-1"></a><span class="fu">write_csv</span>(storms, <span class="st">&quot;storms.csv&quot;</span>)</span>
<span id="cb47-3"><a href="#cb47-3" aria-hidden="true" tabindex="-1"></a><span class="fu">write_csv</span>(storms, <span class="st">&quot;storms.csv.gz&quot;</span>)</span></code></pre></div>
</div>
<div id="总结-1" class="section level3">
<h3>总结</h3>
<p>大部分情况下，当数据整洁时且不涉及时间(日期不影响)时，采用默认参数读取数据即可。当数据集前面行缺失值较多，readr自动猜数据列类型错误时，需要我们人为指定列类型。</p>
<div class="sourceCode" id="cb48"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb48-1"><a href="#cb48-1" aria-hidden="true" tabindex="-1"></a><span class="fu">read_csv</span>(<span class="st">&#39;test.csv&#39;</span>,<span class="at">col_types =</span> <span class="st">&#39;cccnnnnDcnn&#39;</span>)</span></code></pre></div>
<p>由于 Excel 数字位数限制，在输出数字订单号或身份证时需要注意,:</p>
<div class="sourceCode" id="cb49"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb49-1"><a href="#cb49-1" aria-hidden="true" tabindex="-1"></a>id_card <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="st">&#39;440327199910010123&#39;</span>,<span class="st">&#39;440327199910010125&#39;</span>)</span>
<span id="cb49-2"><a href="#cb49-2" aria-hidden="true" tabindex="-1"></a><span class="fu">write_excel_csv</span>(<span class="fu">data.frame</span>(id_card),<span class="st">&#39;test.csv&#39;</span>)</span></code></pre></div>
<blockquote>
<p>Excel 最多只能存储15位数字型数字，15为之后都为0。</p>
</blockquote>
<p>上面的输出显然会改变真实的身份证号码，这时我们就建议输出为xlsx的字符型或者是txt等格式，因为输出CSV会导致后面的全部变成0。</p>
<p>当编码时区不一致时，需指定<code>locale()</code>，如下：</p>
<div class="sourceCode" id="cb50"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb50-1"><a href="#cb50-1" aria-hidden="true" tabindex="-1"></a><span class="fu">read_csv</span>(<span class="fu">readr_example</span>(<span class="st">&quot;mtcars.csv&quot;</span>),<span class="at">locale =</span> <span class="fu">locale</span>(<span class="at">encoding =</span> <span class="st">&#39;UTF-8&#39;</span>,<span class="at">tz =</span> <span class="st">&#39;Asia/Shanghai&#39;</span>))</span></code></pre></div>
</div>
</div>
<div id="data:vroom" class="section level2">
<h2>vroom</h2>
<p>vroom包 实现读取矩形数据到 R 中,如 comma separated(csv),tab separated(tsv), fixed width files(fwf)。该包的功能类似<code>readr::read_csv()</code>,<code>data.table::fread()</code>和<code>read.csv()</code>,但是对于许多数据集来说，<code>vroom::vroom()</code>读取速度会快得多。</p>
<p><a href="https://vroom.r-lib.org/index.html">vroom项目地址</a></p>
<div id="安装" class="section level3">
<h3>安装</h3>
<div class="sourceCode" id="cb51"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb51-1"><a href="#cb51-1" aria-hidden="true" tabindex="-1"></a><span class="co"># 从cran安装</span></span>
<span id="cb51-2"><a href="#cb51-2" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">&quot;vroom&quot;</span>)</span>
<span id="cb51-3"><a href="#cb51-3" aria-hidden="true" tabindex="-1"></a><span class="co"># install.packages(&quot;devtools&quot;)</span></span>
<span id="cb51-4"><a href="#cb51-4" aria-hidden="true" tabindex="-1"></a>devtools<span class="sc">::</span><span class="fu">install_dev</span>(<span class="st">&quot;vroom&quot;</span>)</span></code></pre></div>
</div>
<div id="用法" class="section level3">
<h3>用法</h3>
<ol style="list-style-type: decimal">
<li>读取文件</li>
</ol>
<div class="sourceCode" id="cb52"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb52-1"><a href="#cb52-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(vroom)</span>
<span id="cb52-2"><a href="#cb52-2" aria-hidden="true" tabindex="-1"></a>file <span class="ot">&lt;-</span> <span class="fu">vroom_example</span>(<span class="st">&quot;mtcars.csv&quot;</span>)</span>
<span id="cb52-3"><a href="#cb52-3" aria-hidden="true" tabindex="-1"></a>file</span>
<span id="cb52-4"><a href="#cb52-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb52-5"><a href="#cb52-5" aria-hidden="true" tabindex="-1"></a><span class="fu">vroom</span>(file)</span>
<span id="cb52-6"><a href="#cb52-6" aria-hidden="true" tabindex="-1"></a><span class="fu">vroom</span>(file, <span class="at">delim =</span> <span class="st">&quot;,&quot;</span>)</span></code></pre></div>
<ol start="2" style="list-style-type: decimal">
<li>读取多文件</li>
</ol>
<p>即<code>vroom::vroom()</code>具备迭代效果,具体情况如下：</p>
<div class="sourceCode" id="cb53"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb53-1"><a href="#cb53-1" aria-hidden="true" tabindex="-1"></a>mt <span class="ot">&lt;-</span> tibble<span class="sc">::</span><span class="fu">rownames_to_column</span>(mtcars, <span class="st">&quot;model&quot;</span>)</span>
<span id="cb53-2"><a href="#cb53-2" aria-hidden="true" tabindex="-1"></a>purrr<span class="sc">::</span><span class="fu">iwalk</span>(</span>
<span id="cb53-3"><a href="#cb53-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">split</span>(mt, mt<span class="sc">$</span>cyl),</span>
<span id="cb53-4"><a href="#cb53-4" aria-hidden="true" tabindex="-1"></a>  <span class="sc">~</span> <span class="fu">vroom_write</span>(.x, glue<span class="sc">::</span><span class="fu">glue</span>(<span class="st">&quot;mtcars_{.y}.csv&quot;</span>), <span class="st">&quot;</span><span class="sc">\t</span><span class="st">&quot;</span>)</span>
<span id="cb53-5"><a href="#cb53-5" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb53-6"><a href="#cb53-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb53-7"><a href="#cb53-7" aria-hidden="true" tabindex="-1"></a>files <span class="ot">&lt;-</span> fs<span class="sc">::</span><span class="fu">dir_ls</span>(<span class="at">glob =</span> <span class="st">&quot;mtcars*csv&quot;</span>)</span>
<span id="cb53-8"><a href="#cb53-8" aria-hidden="true" tabindex="-1"></a>files</span>
<span id="cb53-9"><a href="#cb53-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb53-10"><a href="#cb53-10" aria-hidden="true" tabindex="-1"></a><span class="co"># read_csv</span></span>
<span id="cb53-11"><a href="#cb53-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb53-12"><a href="#cb53-12" aria-hidden="true" tabindex="-1"></a>purrr<span class="sc">::</span><span class="fu">map_dfr</span>(files,readr<span class="sc">::</span>read_delim,<span class="at">delim=</span><span class="st">&quot;</span><span class="sc">\t</span><span class="st">&quot;</span>)</span>
<span id="cb53-13"><a href="#cb53-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb53-14"><a href="#cb53-14" aria-hidden="true" tabindex="-1"></a><span class="co"># vroom same above</span></span>
<span id="cb53-15"><a href="#cb53-15" aria-hidden="true" tabindex="-1"></a><span class="fu">vroom</span>(files) </span></code></pre></div>
<ol start="3" style="list-style-type: decimal">
<li>读取压缩文件</li>
</ol>
<p>vroom支持zip,gz,bz2,xz等压缩文件，只需要将压缩文件名称传递给vroom即可。</p>
<div class="sourceCode" id="cb54"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb54-1"><a href="#cb54-1" aria-hidden="true" tabindex="-1"></a>file <span class="ot">&lt;-</span> <span class="fu">vroom_example</span>(<span class="st">&quot;mtcars.csv.gz&quot;</span>)</span>
<span id="cb54-2"><a href="#cb54-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb54-3"><a href="#cb54-3" aria-hidden="true" tabindex="-1"></a><span class="fu">vroom</span>(file)</span></code></pre></div>
<ol start="4" style="list-style-type: decimal">
<li>读取网络文件</li>
</ol>
<div class="sourceCode" id="cb55"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb55-1"><a href="#cb55-1" aria-hidden="true" tabindex="-1"></a><span class="co">#file &lt;- &quot;https://raw.githubusercontent.com/r-lib/vroom/master/inst/extdata/mtcars.csv&quot;</span></span>
<span id="cb55-2"><a href="#cb55-2" aria-hidden="true" tabindex="-1"></a>file <span class="ot">&lt;-</span> <span class="st">&quot;http://www.zhongyufei.com/Rbook/data/data-table-groupingsets.csv&quot;</span></span>
<span id="cb55-3"><a href="#cb55-3" aria-hidden="true" tabindex="-1"></a><span class="fu">vroom</span>(file)</span></code></pre></div>
<ol start="5" style="list-style-type: decimal">
<li>选择列读取</li>
</ol>
<p>room提供了与<code>dplyr::select()</code>相同的列选择和重命名接口</p>
<div class="sourceCode" id="cb56"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb56-1"><a href="#cb56-1" aria-hidden="true" tabindex="-1"></a>file <span class="ot">&lt;-</span> <span class="fu">vroom_example</span>(<span class="st">&quot;mtcars.csv.gz&quot;</span>)</span>
<span id="cb56-2"><a href="#cb56-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb56-3"><a href="#cb56-3" aria-hidden="true" tabindex="-1"></a><span class="fu">vroom</span>(file, <span class="at">col_select =</span> <span class="fu">c</span>(model, cyl, gear))</span>
<span id="cb56-4"><a href="#cb56-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb56-5"><a href="#cb56-5" aria-hidden="true" tabindex="-1"></a><span class="co"># vroom(file, col_select = c(1, 3, 11))</span></span>
<span id="cb56-6"><a href="#cb56-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb56-7"><a href="#cb56-7" aria-hidden="true" tabindex="-1"></a><span class="co"># vroom(file, col_select = list(car = model, everything()))</span></span></code></pre></div>
</div>
</div>
<div id="data:rstudio-addins" class="section level2">
<h2>Rstudio导入</h2>
<p>使用 Rstudio 工具栏自带的 import Dataset 或者借助 R 包插件实现数据与 R 的交互。</p>
<div id="rstudio-工具栏导入" class="section level3">
<h3>rstudio 工具栏导入</h3>
<p>本质也是调用<code>readr</code>和<code>readxl</code>包，如下所示：</p>
<div class="figure">
<img src="picture/read-write/Rstudio-load-data.png" alt="" />
<p class="caption">rstudio-load-data</p>
</div>
</div>
<div id="插件导入" class="section level3">
<h3>插件导入</h3>
<p><a href="https://github.com/milesmcbain/datapasta">项目地址</a></p>
<p>datapasta 是一个 addins 插件，方便将数据复制到 R。</p>
<p>1.安装</p>
<div class="sourceCode" id="cb57"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb57-1"><a href="#cb57-1" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">&quot;datapasta&quot;</span>)</span></code></pre></div>
<p>2.使用</p>
<div class="figure">
<img src="picture/read-write/datapasta-copy.gif" alt="" />
<p class="caption">datapasta</p>
</div>
</div>
</div>
<div id="data:file-path" class="section level2">
<h2>文件路径</h2>
<p>我们读取数据时都是读取某路径下的某文件，但是由于系统等原因，路径在不同系统下的表示方式不一致。</p>
<div id="指定路径" class="section level3">
<h3>指定路径</h3>
<ul>
<li>win 路径</li>
</ul>
<p>windows 系统：<code>C:\Users\zhongyf\Desktop\Rbook</code>，注意路径中是一个反斜杠\。</p>
<div class="figure">
<img src="picture/read-write/win-path.png" alt="" />
<p class="caption">win-path</p>
</div>
<p>在R中读取时需要用一个正斜杠或两个反斜杠，建议用正斜杠。</p>
<div class="sourceCode" id="cb58"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb58-1"><a href="#cb58-1" aria-hidden="true" tabindex="-1"></a>readr<span class="sc">::</span><span class="fu">read_csv</span>(<span class="st">&#39;C:/Users/zhongyf/Desktop/Rbook/data/flights.csv&#39;</span>)</span>
<span id="cb58-2"><a href="#cb58-2" aria-hidden="true" tabindex="-1"></a>readr<span class="sc">::</span><span class="fu">read_csv</span>(<span class="st">&#39;C:</span><span class="sc">\\</span><span class="st">Users</span><span class="sc">\\</span><span class="st">zhongyf</span><span class="sc">\\</span><span class="st">Desktop</span><span class="sc">\\</span><span class="st">Rbook</span><span class="sc">\\</span><span class="st">data</span><span class="sc">\f</span><span class="st">lights.csv&#39;</span>) <span class="co"># same above</span></span>
<span id="cb58-3"><a href="#cb58-3" aria-hidden="true" tabindex="-1"></a>readr<span class="sc">:::</span><span class="fu">read_csv</span>(<span class="at">file =</span> r<span class="st">&quot;(C:\Users\zhongyf\Desktop\Rbook\data</span><span class="sc">\f</span><span class="st">lights.csv)&quot;</span>) <span class="co"># same above</span></span></code></pre></div>
<p>工作中，当需要读取或写入共享盘<a href="#fn4" class="footnote-ref" id="fnref4"><sup>4</sup></a>中文件时，路径表示方式为：</p>
<div class="sourceCode" id="cb59"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb59-1"><a href="#cb59-1" aria-hidden="true" tabindex="-1"></a>the_shared_disk <span class="ot">&lt;-</span> r<span class="st">&quot;(</span><span class="sc">\\</span><span class="st">192.168.2.117\公司A-新</span><span class="sc">\01</span><span class="st">事业部\)&quot;</span></span>
<span id="cb59-2"><a href="#cb59-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb59-3"><a href="#cb59-3" aria-hidden="true" tabindex="-1"></a><span class="co"># 读取共享盘数据</span></span>
<span id="cb59-4"><a href="#cb59-4" aria-hidden="true" tabindex="-1"></a>readr<span class="sc">::</span><span class="fu">read_csv</span>(<span class="at">file =</span> <span class="st">&quot;</span><span class="sc">\\\\</span><span class="st">192.168.2.117</span><span class="sc">\\</span><span class="st">公司A-新</span><span class="sc">\\</span><span class="st">01事业部</span><span class="sc">\\</span><span class="st">flights.csv&quot;</span>)</span>
<span id="cb59-5"><a href="#cb59-5" aria-hidden="true" tabindex="-1"></a><span class="co"># 或者用r&quot;()&quot;指定路径</span></span>
<span id="cb59-6"><a href="#cb59-6" aria-hidden="true" tabindex="-1"></a>readr<span class="sc">::</span><span class="fu">read_csv</span>(<span class="at">file =</span> r<span class="st">&quot;(</span><span class="sc">\\</span><span class="st">192.168.2.117\公司A-新</span><span class="sc">\01</span><span class="st">事业部</span><span class="sc">\t</span><span class="st">est.csv)&quot;</span>)</span></code></pre></div>
<blockquote>
<p>r“()”用法是R-4.0-之后的特性。在win系统下表示路径特别有用，可以避免转义。</p>
</blockquote>
<ul>
<li>mac 路径</li>
</ul>
<p>macOS系统: <code>/User/vega_mac/Desktop/r</code>,路径中是一个正斜杠//。</p>
<div class="figure">
<img src="picture/read-write/mac-path.png" alt="" />
<p class="caption">mac-path</p>
</div>
<div class="sourceCode" id="cb60"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb60-1"><a href="#cb60-1" aria-hidden="true" tabindex="-1"></a>readr<span class="sc">::</span><span class="fu">read_csv</span>(<span class="st">&#39;/User/vega_mac/Desktop/r/Rbook/data/flights.csv&#39;</span>)</span></code></pre></div>
</div>
<div id="默认路径" class="section level3">
<h3>默认路径</h3>
<p>在我们刚开始学习文件读写的时候，都会疑惑输出的文件到哪里去了？在 R 中有一个“当前默认工作目录”的概念，如果不做任何设置，当前默认工作目录一般在：</p>
<p>Windows 系统默认目录一般都是用户文件夹下的文档文件，即./zhongyf/Documents 文件夹下。</p>
<div class="sourceCode" id="cb61"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb61-1"><a href="#cb61-1" aria-hidden="true" tabindex="-1"></a><span class="fu">getwd</span>()</span>
<span id="cb61-2"><a href="#cb61-2" aria-hidden="true" tabindex="-1"></a>[<span class="dv">1</span>] <span class="st">&quot;C:/Users/zhongyf/Documents&quot;</span></span></code></pre></div>
<p>Macos/linxu 默认目录一般都是用户文件夹下，即 /Users/zhongyf 或者是 /home/zhongyf 文件夹下。</p>
<div class="sourceCode" id="cb62"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb62-1"><a href="#cb62-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Mac</span></span>
<span id="cb62-2"><a href="#cb62-2" aria-hidden="true" tabindex="-1"></a><span class="fu">getwd</span>()</span>
<span id="cb62-3"><a href="#cb62-3" aria-hidden="true" tabindex="-1"></a>[<span class="dv">1</span>] <span class="st">&quot;/Users/zhongyf&quot;</span></span>
<span id="cb62-4"><a href="#cb62-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb62-5"><a href="#cb62-5" aria-hidden="true" tabindex="-1"></a><span class="co"># linux</span></span>
<span id="cb62-6"><a href="#cb62-6" aria-hidden="true" tabindex="-1"></a><span class="fu">getwd</span>()</span>
<span id="cb62-7"><a href="#cb62-7" aria-hidden="true" tabindex="-1"></a>[<span class="dv">1</span>] <span class="st">&quot;/home/zhongyf&quot;</span></span></code></pre></div>
<p><code>getwd()</code>是查看当前工作目录的函数，在进行文件读写时的默认路径，也就是当没有明确指定路径时，文件读取输出的默认路径是<code>getwd()</code>。想要改变工作目录，通过设定<code>setwd()</code>即可。 如下所示：</p>
<div class="sourceCode" id="cb63"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb63-1"><a href="#cb63-1" aria-hidden="true" tabindex="-1"></a><span class="co"># not run</span></span>
<span id="cb63-2"><a href="#cb63-2" aria-hidden="true" tabindex="-1"></a><span class="fu">setwd</span>(<span class="st">&#39;C:/Users/zhongyf/Desktop/Rbook/data&#39;</span>)</span></code></pre></div>
<p>除了上述通过<code>setwd()</code>修改工作目录外，可以通过 Rsutdio 的 Tools 工具栏 -&gt; Global Options -&gt; Default working directory 设置工作目录。还可以通过
Rstudio &gt; Session &gt; Set Working Directory &gt; Choose Directory 来调整【快捷键 Ctrl+Shift+H 】。修改工作目录后，通过<code>getwd()</code>查看修改是否生效。</p>
<p>关于文件路径，我个人喜欢的解决办法是通过 File &gt; New Project &gt; New Directory 新建文件项目，将本次所有的数据需求放在该新建的文件夹下，那我们在做本次项目时就再也不用理会文件路径问题了，因为所有的默认路径都是刚新建的路径。这种项目管理的方法，我个人比较推崇。</p>
<blockquote>
<p>配上项目说明 Readme 文件，配合 git 做项目版本控制,方便我们后期查阅回溯。</p>
</blockquote>
</div>
</div>
<div id="data:expand" class="section level2">
<h2>拓展</h2>
<div id="tidyxl:package" class="section level3">
<h3>tidyxl</h3>
<p>tidyxl:Imports non-tabular from Excel files into R。tidyxl 将不规则数据导入R。
我们可以通过<code>readxl</code>包读取整洁的 Excel 数据，那如果是 Excel 的透视表怎么读取？</p>
<p><a href="https://github.com/nacnudus/tidyxl/">tidyxl项目地址</a>。</p>
<p>如果我们想要读取以下数据透视表，该如何读取?由于不是整洁的数据表，所以用<code>readxl</code>读取后的数据没法直接使用。但是<code>tidyxl</code>配合<code>unpivotr</code>包经过处理可以达到目的，如下所示：</p>
<div class="figure">
<img src="picture/read-write/pivottable.png" alt="" />
<p class="caption">PivotTable</p>
</div>
<div class="sourceCode" id="cb64"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb64-1"><a href="#cb64-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyxl)</span>
<span id="cb64-2"><a href="#cb64-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span>
<span id="cb64-3"><a href="#cb64-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(unpivotr)</span>
<span id="cb64-4"><a href="#cb64-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb64-5"><a href="#cb64-5" aria-hidden="true" tabindex="-1"></a>path <span class="ot">&lt;-</span> <span class="st">&#39;./data/read-write/PivotTable.xlsx&#39;</span></span>
<span id="cb64-6"><a href="#cb64-6" aria-hidden="true" tabindex="-1"></a>unpivot <span class="ot">&lt;-</span> <span class="fu">xlsx_cells</span>(path,<span class="at">sheets =</span> <span class="dv">1</span>) <span class="sc">%&gt;%</span> </span>
<span id="cb64-7"><a href="#cb64-7" aria-hidden="true" tabindex="-1"></a>  dplyr<span class="sc">::</span><span class="fu">filter</span>(row<span class="sc">&gt;</span><span class="dv">3</span>) <span class="sc">%&gt;%</span> </span>
<span id="cb64-8"><a href="#cb64-8" aria-hidden="true" tabindex="-1"></a>  dplyr<span class="sc">::</span><span class="fu">filter</span>(<span class="sc">!</span>is_blank) <span class="sc">%&gt;%</span> </span>
<span id="cb64-9"><a href="#cb64-9" aria-hidden="true" tabindex="-1"></a>  <span class="fu">select</span>(row,col,data_type,character,numeric) <span class="sc">%&gt;%</span> </span>
<span id="cb64-10"><a href="#cb64-10" aria-hidden="true" tabindex="-1"></a>  <span class="fu">behead</span>(<span class="st">&#39;left&#39;</span>,区域) <span class="sc">%&gt;%</span> </span>
<span id="cb64-11"><a href="#cb64-11" aria-hidden="true" tabindex="-1"></a>  <span class="fu">behead</span>(<span class="st">&#39;up&#39;</span>,是否可比) <span class="sc">%&gt;%</span> </span>
<span id="cb64-12"><a href="#cb64-12" aria-hidden="true" tabindex="-1"></a>  <span class="fu">behead</span>(<span class="st">&#39;up-right&#39;</span>,新老店) <span class="sc">%&gt;%</span> </span>
<span id="cb64-13"><a href="#cb64-13" aria-hidden="true" tabindex="-1"></a>  <span class="fu">rename</span>(销售额 <span class="ot">=</span> numeric) <span class="sc">%&gt;%</span> </span>
<span id="cb64-14"><a href="#cb64-14" aria-hidden="true" tabindex="-1"></a>  <span class="fu">select</span>(<span class="sc">-</span>character)   </span>
<span id="cb64-15"><a href="#cb64-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb64-16"><a href="#cb64-16" aria-hidden="true" tabindex="-1"></a>unpivot</span></code></pre></div>
<p>经过上述处理，已将透视表数据转化为整洁的数据。</p>
<blockquote>
<p>如果有可能的话，不要读取如此不规则的数据当作数据源</p>
</blockquote>
</div>
<div id="参考资料" class="section level3">
<h3>参考资料</h3>
<ol style="list-style-type: decimal">
<li><p>feather 项目地址<a href="https://github.com/wesm/feather" class="uri">https://github.com/wesm/feather</a></p></li>
<li><p>qs 提供接口，用于快速将R对象保存到磁盘以及从磁盘读取。该包的目标是替换R中的<code>saveRDS</code>和<code>readRDS</code>。项目地址<a href="https://github.com/traversc/qs" class="uri">https://github.com/traversc/qs</a></p></li>
<li><p>arrow 是 feather 的接替项目，地址<a href="https://arrow.apache.org/docs/r/" class="uri">https://arrow.apache.org/docs/r/</a></p></li>
<li><p>其它统计学软件数据如 spss，stata,SAs 等可用<code>foreign</code>包读取</p></li>
<li><p>tidyxl and unpivotr: <a href="https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-simple.html" class="uri">https://nacnudus.github.io/spreadsheet-munging-strategies/pivot-simple.html</a></p></li>
<li><p>janitor package <a href="https://sfirke.github.io/janitor/index.html" class="uri">https://sfirke.github.io/janitor/index.html</a></p></li>
</ol>
</div>
</div>
</div>
<div class="footnotes">
<hr />
<ol>
<li id="fn1"><p>由于 R 是将数据加载至内存中，故数据集大小超过内存大小将导入失败。<a href="#fnref1" class="footnote-back">↩︎</a></p></li>
<li id="fn2"><p>csv 即 Comma-Separated Values，逗号分隔值，分隔符也可是不是逗号。
csv 文件是一种以纯文本形式存储的表格数据,可以通过记事本打开。与 Excel 不同的是，CSV 是一种文本格式，也不受 Excel 最大行数(1048576)限制。
csv 文件也被称作平面文件，结构简单，平面文件比结构文件占用更少的空间；平面文件在数据仓库项目中广泛用于导入数据。<a href="#fnref2" class="footnote-back">↩︎</a></p></li>
<li id="fn3"><p>矩形数据英文中表示为 rectangular data，矩形数据每一列都是变量(特征)，而每一行都是案例或记录,关系数据库中的单表就是矩形数据的一种。<a href="#fnref3" class="footnote-back">↩︎</a></p></li>
<li id="fn4"><p>共享盘的地址即某电脑(服务器)的地址，知道共享盘在局域网中的ip地址后在 开始-&gt;运行中输入\192.168.1.247即可打开共享盘。<a href="#fnref4" class="footnote-back">↩︎</a></p></li>
</ol>
</div>
